简体   繁体   中英

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM