1

I need to click on specified li element using dropdown list: All subject areas on website. The problem is: cannot click on specified li element using Selenium.

For example, if I'm writting like:

 String textInput = "Agricultural and Biological Sciences";  

 //...

 // open the dropdown so the options are visible
 driver.findElement(By.className("dropdown"));

 // Get all of the options of dropdown list
 WebElement ulElement  = driver.findElement(By.cssSelector("ul.dropdown-options.dropdown-element"));
 List<WebElement> opts = ulElement.findElements(By.xpath(".//li/a"));

And trying to choose specified li element:

// Loop through the options and select the one that matches
        for (WebElement opt : opts) {
            if(opt.getText().equals(textInput)){
               opt.click();
               //...
            }
        }

The condition is simply skipped by the program.

If I changed to variant like:

  // Loop through the options and select the one that matches
        for (WebElement opt : opts) {
            if(!opt.findElements(By.xpath("//*[contains(text(), '" + textInput + "')]")).isEmpty()) {
                opt.click();
                //...
            }
        }

The condition isn't ignored and successfully passes, but then the program doesn't click on the button, and the list is closed.

Can someone suggest me how to solve the problem here?

Guy
  • 46,488
  • 10
  • 44
  • 88
invzbl3
  • 5,872
  • 9
  • 36
  • 76

2 Answers2

1

First, to appear the li you need click this element:

driver.findElement(By.className("dropdown")).click();

Second, you need add wait after click and add break statement in the loop:

String textInput = "Agricultural and Biological Sciences";

WebElement ulElement  = driver.findElement(By.cssSelector("ul.dropdown-options.dropdown-element"));
List<WebElement> opts = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfNestedElementsLocatedBy(ulElement, By.xpath(".//li/a")));

//here, before click, just make sure your `li` printed all the text
for(WebElement opt :opts) {
    System.out.println("get text : " +opt.getText());
}

for (WebElement opt : opts) {
    if(opt.getText().equals(textInput)){
        opt.click();
        //here
        break;
    }
}

After you have successfully clicked the one you want, you have switched to another page, so the li element after that no longer exists. So you need a break statement.

frianH
  • 7,295
  • 6
  • 20
  • 45
  • Thanks for answer, frianH. I've added `click` for `dropdown` and it chooses the input field fine, but still the condition: `if(opt.getText().equals(textInput)){` doesn't work as expected, it doesn't want to check my String, so I can't go inside it to use `break`. I've tried to change to: `if (!opt.findElements(By.xpath("//*[contains(text(), '" + textInput + "')]")).isEmpty()) {` and in this case I can go inside, but options are not pressed even with `break`. – invzbl3 Feb 24 '20 at 15:46
  • @invzbl3 I've tried the code and working fine for me, maybe you need `wait` after click the `dropdown`. Btw, glad it worked by other answer. – frianH Feb 25 '20 at 05:35
  • Did you change something besides adding `click()` method and `break` inside if-condition? Was your String the same as mine? – invzbl3 Feb 25 '20 at 14:27
  • 1
    @invzbl3 simply, you can add `Thread.sleep(1000);` after clicked the `dropdown`, but it's bad idea. I've updated the code, please look the above, it's what I've done to try solve your issue. I'm just suspicious after you clicked `dropdown`, the text you meant isn't ready yet. Hope this helps. – frianH Feb 26 '20 at 04:00
  • thanks, I've tested again and now it works, but after you added `WebDriverWait` also. – invzbl3 Feb 29 '20 at 18:26
1

To click() on the element with text as Agricultural and Biological Sciences (miscellaneous) from the dropdown you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategies:

  • xpath:

    driver.navigate().to("https://www.scimagojr.com/journalrank.php?country=UA&page=1");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[normalize-space()='All subject areas']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[normalize-space()='All subject areas']//following-sibling::ul[1]//li/a[normalize-space()='Agricultural and Biological Sciences']"))).click();
    
  • Browser Snapshot:

scimagojr

invzbl3
  • 5,872
  • 9
  • 36
  • 76
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks, DebanjanB. It works as expected. A bit modified solution to another `dropdown` list. – invzbl3 Feb 24 '20 at 15:47
  • 1
    @invzbl3 Your code attempts were clear enough to understand what was missing, so the answer had to work. Thanks for the well structured question. – undetected Selenium Feb 24 '20 at 15:50