简体   繁体   中英

How do I select an option inside an <li> tag using Selenium Webdriver in java?

I tried using cssSelector and using xpath but could not find how to select one value from the list.

r.driver.findElement(By.xpath("//div[contains(.,'DOLO -650')]"));
r.driver.findElement(By.className("ui-autocomplete ui-front ui-menu ui-widget ui-widget-content")).click(); 
List<WebElement> opt = r.driver.findElements(By.xpath("//ul[@class='ui-autocomplete ui-front ui-menu ui-widget ui-widget-content']/li")); 

for (WebElement options:opt) {
    if(options.getText().equals(opt)) {
        options.click(); return;
    } 
} 

WebElement dd = r.driver.findElement(By.id("ui-id-1")); dd.click(); 
dd.findElement(By.cssSelector("li[value="+ Product+"]")).click();
r.driver.findElement(By.cssSelector("li:nth-child(1).NoBulle‌​t.jms-bullet>")).cli‌​ck();
WebElement element = r.driver.findElement(By.name("DOLO -650")); 
element.sendKeys("DOLO -650"); 
element.submit(); 

<ul id="ui-id-1" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content"> 
    <li id="ui-id-6" class="ui-menu-item">DOLO -120 60ML</li> 
    <li id="ui-id-7" class="ui-menu-item ui-state-focus">DOLO -650</li>
    <li id="ui-id-8" class="ui-menu-item">DOLO 100ML</li>
</ul>

You have to try below code to get all li element and select which matches with your expectation

List<WebElement> opt = driver.findElements(By.xpath("//ul[@id='ui-id-1']/li")); 

for(WebElement options:opt)
{
    if(option.getText().equals("DOLO -650")) 
    {
        options.click(); 
        break;
    }
}
WebElement autoOptions= driver.findElement(By.id("ui-id-1"));
autoOptions.sendKeys("DOLO");
List<WebElement> opt = driver.findElements(By.xpath("//ul[@class='ui-autocomplete ui-front ui-menu ui-widget ui-widget-content']/li")); 

for(WebElement option:opt)
{
    if(option.getText().equals("DOLO -650")) 
    {
        System.out.println(option);
        option.click(); 
        break;
    }
}

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