繁体   English   中英

Selenium WebDriver-Java。 请帮忙从列表中选择项目吗?

[英]Selenium WebDriver - Java. Please help selecting item from list by?

这是html的示例。 对于所有“下拉列表”,该应用程序都是这样的。 单击它,然后出现选项。 我希望能够创建此方法,并在一个单独的类中能够调用该方法,使其能够调用它(chooseState(“ California”);)并传递状态变量,然后选择状态。

<ul unselectable="on" class="k-list k-reset" tabindex="-1" role="listbox" aria-hidden="true" id="InputModel_State_listbox" aria-live="off" style="overflow: auto;">
    <li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" id="InputModel_State_option_selected" aria-selected="true">--- select ---</li>
    <li tabindex="-1" role="option" unselectable="on" class="k-item"/>
    <li tabindex="-1" role="option" unselectable="on" class="k-item">statename1</li>
    <li tabindex="-1" role="option" unselectable="on" class="k-item">statename2</li>
    <li tabindex="-1" role="option" unselectable="on" class="k-item">statename50</li>
</ul>

这是我尝试创建的方法的示例,该方法将允许您通过状态变量获取状态名称

public void chooseState(String state) {
        try {


                         //This clicks the "Dropdown
            driver.findElement(By.cssSelector(State)).click();

                        //I need the Method to choose by state name.  I know I am sort of on the right track because I am able to click the first item I just need to be able to pass any state and it choose the correct one.

                        **Here are examples of things that failed**

driver.findElement(By.cssSelector(State)).click();
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText(state))).click();

Select select = new Select(driver.findElement(By
                .id("InputModel_State_listbox")));
        select.deselectAll();
        select.selectByVisibleText("state");

Select select = new Select(findElement(by));
select.selectByVisibleText(state); }




        } catch (NoSuchElementException e) {
            log.error("Fail ", e);
            Assert.fail();
        }
    }

如果我正确理解了您的问题,那么您想知道定位器/标识符以单击li项目的特定状态名称。

您可以为此使用XPath代替cssSelector(),如下所示。

driver.findElement(By.xPath("//li[text()='" + State + "']")).click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM