【问题标题】:Unable to select item from dropdown list in Selenium Webdriver无法从 Selenium Webdriver 的下拉列表中选择项目
【发布时间】:2018-07-30 22:30:31
【问题描述】:

我正在使用 Selenium Webdriver 2.41 版和 Chrome 68 版。 我正在尝试从下拉列表中获取一项:

<div id="loc_placeholder" class="jLocPlaceholder" style="">All locations</div>
    <select id="location_facet" name="location_facet" class="search_input font_bold multiselect jLocInput chzn-done" data-placeholder="All locations" multiple="" style="display: none;">
      <option value="233">United Kingdom</option>
      <option value="250">Netherlands</option>
      <option value="228">United States</option>

等等

我的问题是:为什么这段代码不起作用?我用其他选项尝试了很多次,但驱动程序仍然无法选择任何选项:/

 WebElement location = driver.findElement(By.id("loc_placeholder"));
 location.click();

 Select dropdown = new Select(driver.findElement(By.id("location_facet")));
 dropdown.selectByValue("250");
 //dropdown.selectByIndex(1);     
 //dropdown.selectByVisibleText("Netherlands"); 

我正在点击下拉列表并推出列表。

依赖项是:

 <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.13.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

我收到错误:

org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated

(会话信息:chrome=68.0.3440.75) (驱动程序信息:chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:0 毫秒 构建信息:版本:'3.11.0',修订:'e59cfb3',时间:'2018-03-11T20:26:55.152Z'

【问题讨论】:

  • 您在尝试选择值时是否遇到任何错误/异常?您是否可以将 selenium 库和 chrome 驱动程序 exe 更新到最新版本并重试?
  • 下拉列表有style="display: none;。单击第一个元素后它会改变吗?
  • 我收到一个错误:org.openqa.selenium.ElementNotVisibleException:元素不可见:元素当前不可见,可能无法操作(会话信息:chrome=68.0.3440.75)(驱动程序信息:chromedriver =2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64) (警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:0 毫秒构建信息:版本:'3.11.0',修订:' e59cfb3',时间:'2018-03-11T20:26:55.152Z'
  • 由于下拉菜单最初是隐藏的,请尝试等待它可见/可点击
  • 你的意思是使用Thread.sleep(1000);例如 ?其实我试过了,不幸的是它没有帮助:(

标签: java selenium-webdriver


【解决方案1】:

感谢您的帮助! :) 我将style="display: none; 修改为block,之后它就可见了。

((JavascriptExecutor)driver).executeScript("jQuery('#location_facet').css('display','block')");
        Select select = new Select(driver.findElement(By.id("location_facet")));
        select.selectByVisibleText("Netherlands");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2015-05-19
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多