Warm tip: This article is reproduced from stackoverflow.com, please click
automated-tests cucumber eclipse selenium

click on an expand & collapse button selenium cucumber eclipse

发布于 2020-04-20 10:03:50

I am trying to click on an expand/collapse button and my first code is working for one of the buttons but somehow it is not working for the other two. It is the same structure that is why it is making this as a strange problem.

Html 1

<!-- language: lang-html -->

<button class="collapser__header collapsed " type="button" data-toggle="collapse" data-target="#collapseIndkomstrubrikker" aria-expanded="false" aria-controls="collapseIndkomstrubrikker">Anden indkomst</button>

<div class="collapse" id="collapseIndkomstrubrikker"></div>

<!-- end snippet -->

Code which is not working

case "Anden indkomst": 
            try {
                WebElement wb;
                JavascriptExecutor js = (JavascriptExecutor)driver;
                    wb = driver.findElement(By.xpath("//*[@data-target='#collapseIndkomstrubrikker']"));
                    js.executeScript("arguments[0].click();", wb);
                } catch (Exception b) {
                   throw new NoSuchElementException("kunne ikke finde " + knap);

                }
            break;    

Html 2

<button class="collapser__header collapsed " type="button" data-toggle="collapse" data-target="#collapseFradragrubrikker" aria-expanded="false" aria-controls="collapseFradragrubrikker">Andre fradrag</button>

<div class="collapse" id="collapseFradragrubrikker"></div>

Code 2 which is working

case "Andre fradrag": 
            try {
                WebElement wb;
                JavascriptExecutor js = (JavascriptExecutor)driver;
                    wb = driver.findElement(By.xpath("//*[@data-target='#collapseFradragrubrikker']"));
                    js.executeScript("arguments[0].click();", wb);
                } catch (Exception b) {

               throw new NoSuchElementException("kunne ikke finde " + knap);
            }
        break;
Questioner
rtgrt
Viewed
435
rtgrt 2020-02-12 20:47

There was smth wrong with my Eclipse which is the reason why my code didn't work in the first place. When I restarted Eclipse the code worked:)