我是硒的新手。我很难从下拉菜单中选择元素。我正在使用PhantomJS实现它 - 使用Firefox作为Webdriver的相同代码使用PhantomJS无法工作。我需要使用PhantomJS开发代码以在VM上运行它。
环境设置{" browserName":" phantomjs","版本":" 2.1.1"," driverName&# 34;:" ghostdriver"" driverVersion":" 1.2.0""平台":"窗户-7-32bit& #34;," selenium version":" 3.0.1 2016-10-18"}
这是我尝试过但没有用的东西:
这些方法都没有 - 有些在stackoverflow上找到 - 对我有用。请帮我找一个解决方案;希望这个问题对其他人也有用。
提前致谢!
示例代码1:
Select dropdownAvailableAverageDroveKM = new Select(driver.findElement(By.id("sel_percorrenza")));
dropdownAvailableAverageDroveKM.selectByIndex(1);
示例代码2:
driver.findElement(By.xpath("//input[@value='10.000 - 20.000']")).click();
driver.findElement(By.xpath("//li[@data-value='01']")).click();
示例代码3:
Select dropdownAvailableAverageDroveKM = new Select(driver.findElement(By.id("sel_percorrenza")));
List<WebElement> percorrenze = dropdownAvailableAverageDroveKM.getOptions();
for (int i = 0; i < percorrenze.size(); i++) {
if (i == 2) {
percorrenze.get(i).click();
}
}
示例代码4:
driver.findElement(By.id("sel_percorrenza")).sendKeys("0 - 10.000");
这里是HTML:
<div class="selectContainer select-wrapper required invalid">
<span class="material-icons caret">arrow_drop_down</span><input type="text" class="select-dropdown ipt-sel_percorrenza valid" data-activates="select-options-6e6bc713-38a9-6d84-6d1d-4f98ac98a935" value="10.000 - 20.000" readonly="" data-search-options="false" placeholder="" aria-invalid="false">
<ul id="select-options-6e6bc713-38a9-6d84-6d1d-4f98ac98a935" class="dropdown-content select-dropdown" style="width: 269px; position: absolute; top: 0px; left: 0px; opacity: 1; display: none;">
<li class="active" data-value=""><span>Seleziona</span></li>
<li class="" data-value="01"><span>0 - 10.000</span></li>
<li class="" data-value="02"><span>10.000 - 20.000</span></li>
<li class="" data-value="03"><span>20.000 - 30.000</span></li>
<li class="" data-value="04"><span>oltre 30.000</span></li>
</ul>
<select name="oggettoAssicuratoModel.veicolo.percorrenza" class="required initialized invalid" id="sel_percorrenza" aria-required="true" aria-invalid="true">
<option value="">Seleziona</option>
<option value="01">0 - 10.000</option>
<option value="02" selected="selected">10.000 - 20.000</option>
<option value="03">20.000 - 30.000</option>
<option value="04">oltre 30.000</option>
</select>
</div>
答案 0 :(得分:0)
作为例外,您需要设置窗口大小:
driver.manage().window().setSize(new Dimension(1366, 768)); // fake browser to do something on GUI
希望有帮助。
答案 1 :(得分:0)
尝试
Select dropdown = new Select(driver.findElement(By.xpath("//select[contains(@id,'sel_percorrenza')]")));
dropdown.selectByVisibleText("0 - 10.000");
希望这对您有帮助