在显示中选择选项时出现问题:无下拉Selenium

时间:2017-07-17 06:04:35

标签: java selenium drop-down-menu

我不会轻易提出这个问题,但事实证明它比我想象的更有问题。我通过谷歌搜索可能的解决方案,但似乎我无法从下拉列表中选择一个特定的选项。

如果有人可以给我一个指针或解决方案,那将是非常受欢迎的,因为我正在试着让它发挥作用。

这是html:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="toggle">Open box at side</button>
  <div id="link-box">
    <ul>
      <li><a href="#">Link 1</a></li>
      <li><a href="#">Link 2</a></li>
      <li><a href="#">Link 3</a></li>
      <li><a href="#">Link 4</a></li>
    </ul>

我尝试了以下内容:

<div id="main-content" class="col-xs-12">
<div class="row">
    <div
        class="form-horizontal col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4">
        <div class="col-xs-12">
            <h1>Please sign in</h1>
            <div class="form-group form-group-sm">
                <div class="form-group">
                    <div class="form-group">
                        <div class="form-group" data-bind="visible:loginExternal"
                            style="display: none;">
                            <div class="form-group">
                                <span class="k-widget k-dropdown k-header" style="width: 100%;"
                                    unselectable="on" role="listbox" aria-haspopup="true"
                                    aria-expanded="false" tabindex="0" aria-owns=""
                                    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> <select data-role="dropdownlist" data-text-field="display"
                                    data-option-label="Please select ..."
                                    style="width: 100%; display: none;"
                                    data-value-primitive="true" data-value-field="irn"
                                    data-bind="source: locations, value: selectedLocation">
                                        <option value="822">Alexandra</option>
                                        <option value="800">Ridge</option>
                                        <option value="896">Ture</option>
                                        <option value="899">Grove</option>
                                        <option value="824">Lea</option>
                                        <option value="825">Mount</option>
                                        <option value="894">Cliffe</option>
                                        <option value="788">Bank</option>
                                        <option value="826">Ponga</option>
                                        <option value="259">Post</option>
                                </select>
                                </span>
                            </div>
                            <div class="form-group">
                                <span class="k-widget k-dropdown k-header" style="width: 100%;"
                                    unselectable="on" role="listbox" aria-haspopup="true"
                                    aria-expanded="false" aria-owns="loginSubLoc_listbox"
                                    aria-disabled="true" aria-readonly="false" aria-busy="false"
                                    aria-activedescendant="loginSubLoc_option_selected"> <span
                                    class="k-dropdown-wrap k-state-disabled" unselectable="on">
                                        <span class="k-input" unselectable="on"></span> <span
                                        class="k-select" unselectable="on"> <span
                                            class="k-icon k-i-arrow-s" unselectable="on">select</span>
                                    </span>
                                </span> <select id="loginSubLoc" data-role="dropdownlist"
                                    data-text-field="display"
                                    data-option-label="Please select ..."
                                    style="width: 100%; display: none;"
                                    data-value-primitive="true" data-value-field="irn"
                                    data-bind="source: sublocations, value: selectedsubLocation, enabled: isSublocationEnabled"
                                    disabled="disabled">
                                        <option value="">Please select ...</option>
                                        <option value="0" selected="selected"></option>
                                </select>
                                </span>
                            </div>
                            <div class="form-group">
                                <button id="submit" class="btn btn-lg btn-primary btn-block"
                                    type="button" data-bind="click: btnLoginClicked">Login</button>
                            </div>

我收到错误:元素不可见:元素当前不可见,可能无法操作

我也尝试了以下内容:

WebElement element = driver.findElement(By.cssSelector("div>span>select[data-bind='source: locations, value: selectedLocation']"));
   Select dropdown = new Select(element);
   driver.findElement(By.cssSelector("div.form-group>span>span.k-dropdown-wrap.k-state-default>span.k-input")).click();
   dropdown.selectByValue("822");

它正确打印出选项值,但无法点击我想要的选项。我收到一个错误:org.openqa.selenium.ElementNotVisibleException:元素不可见

非常令人沮丧。不确定如何推动这一个。

任何帮助都会非常感激,因为我现在依靠Robot类操作下拉列表并希望避免使用此方法。

2 个答案:

答案 0 :(得分:0)

第一种方式:使用相同的js单击任何元素不是问题。如您所知,如何获取任何选项,最后的操作是执行单击。这应该适合你:

WebElement hiddenWebElement =driver.findElement(By(..selector of the element....));
        ((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);

第二路:

JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'#subElement\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());

第三种方式:使用操作构建器,高级用户操作API。

WebElement mnEle;
WebElement sbEle;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();

Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();

希望这会对你有所帮助:)。

答案 1 :(得分:0)

以下是您的问题的答案:

根据您的问题,要选择<option value="822">Alexandra</option>选项,您可以考虑尝试以下代码:

WebElement select_dropdownlist = driver.findElement(By.xpath("//*[@data-role='dropdownlist' and @data-text-field='display']"));
Select dropdownlist = new Select(select_dropdownlist);
dropdownlist.selectByVisibleText("Alexandra");
  

OR

WebElement select_dropdownlist = driver.findElement(By.xpath("//*[@data-role='dropdownlist' and @data-option-label='Please select ...']"));
Select dropdownlist = new Select(select_dropdownlist);
dropdownlist.selectByValue("822");   

如果这回答您的问题,请告诉我。