我在li
和div
下有很多ul
个标签。每个li
代码都包含strong
代码:
<div style="width:380px" id="autoComplete" class="suggest fl">
<div class="sWrap">
<div class="iconWrap">
<span class="nLoder" style="display: none;"></span>
</div>
<div>
<input type="text" autocomplete="off" placeholder="Search" class="" id="keywords1" style="width:375px;" name="KEYWORDS">
</div>
</div>
<div class="sugCont nScroll " id="sugDrp_autoComplete" style="display: none; width: 380px;">
<ul class="Sdrop">
<li class="sugTouple">
<button style="width:100%" class="Sbtn " tabindex="-1" type="button">
Acc
<strong>ounting</strong>
</button>
</li>
<li class="sugTouple">
<button style="width:100%" class="Sbtn" tabindex="-1" type="button">
Acc
<strong>ounts Payable</strong>
</button>
</li>
<li class="sugTouple">
<button style="width:100%" class="Sbtn" tabindex="-1" type="button">
Acc
<strong>ounts Receivable</strong>
</button>
</li>
<li class="sugTouple">
<button style="width:100%" class="Sbtn" tabindex="-1" type="button">
Acc
<strong>ount Management</strong>
</button>
</li>
</ul>
</div>
</div>
我目前的代码:
List<WebElement> optionsToSelect = driver.findElements(By.xpath("//ul[@class='Sdrop']"));
for (WebElement option : optionsToSelect) {
System.out.println(option);
if (option.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
option.click();
break;
}
}
如何选择值?我错过了一些东西,引导我到达。
答案 0 :(得分:1)
你应该计算li然后开始循环并检查按钮文本这里是更新的代码。
List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@id='sugDrp_autoComplete']/ul[@class='Sdrop']/li"));
for (WebElement option : optionsToSelect) {
WebElement buttonObj = option.findElement(By.tagName("button"))
if (buttonObj.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
buttonObj .click();
break;
}
}
答案 1 :(得分:0)
您可以使用xpath直接单击该按钮:
//button[@class='Sbtn' and text()='textToSelect']