我正在使用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),平台= Windows NT 10.0.17134 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:0毫秒 构建信息:版本:'3.11.0',版本:'e59cfb3',时间:'2018-03-11T20:26:55.152Z'
答案 0 :(得分:0)
感谢您的帮助! :)我将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");