我在java中使用黄瓜和硒自动化网页,我有这个xpath
.//*[@id='chosen_motivos_investimentos_chosen']/div[1]/ul[1]
并且在ul [1]里面我有一堆li,在这种情况下我有这部分的html:
<ul class="chosen-results">
<li class="active-result" style="" data-option-array-index="1">LA - Media Buying Fees ‐ Traditional</li>
<li class="active-result" style="" data-option-array-index="2">LL - Marketing Development</li>
<li class="active-result" style="" data-option-array-index="3">LQ - Media Buying Fees ‐ Digital</li>
<li class="active-result" style="" data-option-array-index="4">LU - Media Costs ‐ Traditional</li>
<li class="active-result" style="" data-option-array-index="5">LV - Advertising Production Costs ‐ Traditional</li>
</ul>
我想点击特定的li进行选择,这个具体是通过参数(String despesa)给出的,我试过这个:
Thread.sleep(2000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='chosen_motivos_investimentos_chosen']/a"))).click();
List<WebElement> dropDespesa = driver.findElements(By.xpath(".//*[@id='chosen_motivos_investimentos_chosen']/div[1]/ul[1]"));
i = dropDespesa.iterator();
while(i.hasNext()){
WebElement row = i.next();
if(row.getText().equalsIgnoreCase(despesa)){
System.out.println("Igual");
row.click();
}
System.out.println(row.getText());
}
但是webdriver永远不会进去,如果,我已经调试并确保despesa实际上等于row.getText()
答案 0 :(得分:0)
您可以按文字找到所需的li
并点击它:
// open up the dropdown
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='chosen_motivos_investimentos_chosen']/a"))).click();
WebElement dropDespesa = driver.findElement(By.xpath(".//*[@id='chosen_motivos_investimentos_chosen']/div[1]/ul[1]"));
dropDespesa.findElement(By.xpath(".//li[. = '" + despesa + "']")).click();