Ezz94
Ezz94

Reputation: 35

How can I click on the li Item using Selenium and Java?

How can I select list items that are nested like this:

<li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation">
<div class="select2-result-label" id="select2-result-label-36" role="option"><span class="select2-`match"></span>
Risk
</div>
</li>

Because each list items differs only from the Text it has.

Upvotes: 1

Views: 489

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193258

To click() on the element with text as Risk you can use either of the following Locator Strategies:

  • xpath:

    driver.findElement(By.xpath("//li[@class='select2-results-dept-0 select2-result select2-result-selectable']/div[@class='select2-result-label' and contains(., 'Risk')]")).click();
    

However, as the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='select2-results-dept-0 select2-result select2-result-selectable']/div[@class='select2-result-label' and contains(., 'Risk')]"))).click();
    

Upvotes: 1

Related Questions