如何在java中使用Selenium Webdriver在<li>标签内选择一个选项?

时间:2017-10-16 07:04:42

标签: java selenium

我尝试使用cssSelector并使用xpath但无法找到如何从列表中选择一个值。

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>

2 个答案:

答案 0 :(得分:0)

您必须尝试下面的代码来获取所有li元素并选择符合您期望的匹配

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;
    }
}

答案 1 :(得分:0)

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;
    }
}