在自动完成栏中,如何单击div ul li strong下的特定值

时间:2016-04-13 10:28:37

标签: java html selenium xpath selenium-webdriver

我在lidiv下有很多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;
    }
}

如何选择值?我错过了一些东西,引导我到达。

2 个答案:

答案 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']