如何在不使用Select类的情况下从Dropdown中选择值,如果在下拉列表中有下拉列表而没有选择?

时间:2016-04-25 18:28:25

标签: selenium select webdriver dropdown

My HTML code is here:
    <fieldset>
<div class="clearfix">
<div class="clearfix">
<div class="clearfix">
<div class="clearfix">
<div class="qs-formfield-short qs-required">
<label for="stateCountry">State or Province</label>
<span class="k-widget k-dropdown k-header" style="" title="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="stateCountry_listbox" aria-disabled="false" aria-readonly="false" aria-busy="false">
<span class="k-dropdown-wrap k-state-default" unselectable="on">
<span class="k-input" unselectable="on">Please Select...</span>
<span class="k-select" unselectable="on">
<span class="k-icon k-i-arrow-s" unselectable="on">select</span>
</span>
</span>

我的代码在这里:

WebElement stateDropDown = driver.findElement(                 By.xpath(&#34; / HTML /体/形式/ DIV [3] /主/ DIV / DIV / DIV /跨度/ DIV /字段集/ DIV [4] / DIV [1] /量程/跨度[1] &#34));                 列表选项= stateDropDown.findElements(By.xpath(&#34; / html / body / div [1] / div / ul / li [44]&#34;));

    for(WebElement opt : options){
        if ("Texas".equals(opt.getText()));
        opt.click();
        System.out.println(opt);

1 个答案:

答案 0 :(得分:0)

尝试这样的smth:

Actions action = new Actions(driver);    
WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'k-dropdown-wrap')]"));
action.moveToElement(optionsList);

List<WebElement> options = driver.getElemets(By.xpath("//span[contains(@class, 'k-input')]"));
for(WebElement option : options) {
    if (option.getText().equals("Texas")) {
        option.click();
    }
}

停止向元素编写奇怪的xpath:)