Java WebDriver无法从JQuery下拉列表中选择
我有一个包含JQuery下拉列表的web表单。特定字段保存出生日期。该字段的来源是:Java WebDriver无法从JQuery下拉列表中选择,java,jquery,selenium,webdriver,Java,Jquery,Selenium,Webdriver,我有一个包含JQuery下拉列表的web表单。特定字段保存出生日期。该字段的来源是: <div class="tooltipGroup" style="z-index:19;"> <div class="day"> <div class="jqTransformSelectWrapper" style="z-index: 19;"> <div> <ul style="width: 100%; d
<div class="tooltipGroup" style="z-index:19;">
<div class="day">
<div class="jqTransformSelectWrapper" style="z-index: 19;">
<div>
<ul style="width: 100%; display: block; visibility: visible;">
<li class="optHeading">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<a index="6" href="#">6</a>
</li>
<li class="undefined">
<a index="31" href="#">31</a>
</li>
这是试图获取所有元素并将其放入HashMap的代码:
public void selectDob(int dob) {
WebElement dobFieldDropdown;
WebElement content = driver.findElement(By.className("leftClmn"));
driver.findElement(By.id("aWrapper_dob_day")).click();
dobFieldDropdown = content.findElements(By.className("tooltipGroup")).get(2).findElement(By.className("day")).findElement(By.tagName("ul"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown.findElements(By.tagName("a"))) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText());
}
dropdownValues.get(dob).click();
}
该代码工作正常,但有一个例外:它无法获取所有字段的值,只有在打开下拉列表时第一个可见的字段
123445
问题是如何获取其他字段的值?您无法使用Web驱动程序定位不可见的元素,您需要使用JavaScript才能获取它们。所以试试类似的方法
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("get them here by class name");
尝试下面的代码并检查它是否工作
WebElement w = driver.findElement(By.id("aWrapper_dob_day"));
w.click();
WebElement dobFieldDropdown = driver.findElements(By.className("undefined"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText());
}
我要感谢你们的帮助,尤其是亨切 事实证明,这是FirefoxDriver 2.32最新版本中的一个bug。同样的代码在ChromeDriver上运行得很好,我得到了所有的drropdown值。将selenium版本降级到2.31解决了这个问题,代码可以与这两个驱动程序一起工作 我将在Selenium bug追踪器上注册一个bug 这是我的代码的最终版本:
public void selectFromDropdown(String option) {
WebElement dobFieldDropdown;
WebElement content = driver.findElement(By.className("leftClmn"));
driver.findElement(By.id("aWrapper_dob_day")).click();
dobFieldDropdown = content.findElements(By.className("tooltipGroup")).get(2).findElement(By.className("day")).findElement(By.tagName("ul"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown.findElements(By.tagName("a"))) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText().toString());
}
dropdownValues.get(option).click();
}
干杯 也许JavascriptExecutor会帮上忙?你能给我们看一下网页或任何类似的网页吗?如果是这样的话。Webdriver获取下拉列表中的所有值,但您只提到了第一个可见值。所以我只想看到这样一个页面,在我们向下滚动更多选项之前,某些选项将不可见。@HemChe是的,在我的例子中,有一个滚动列表列出所有值。当你打开下拉列表时,只会显示前4个选项。我尝试过这种方法,但我得到了错误方法executeScriptString,Object。。。在类型中,JavascriptExecutor不适用于WebElement el的参数列表:dobFieldDropdown.findElementsBy.tagNameul{js.executeScriptdobFieldDropdown.findElementsBy.tagNamea;dropdownValues.putel.getText,el;System.out.printlnel.getText;}否,您需要在JavaScript中搜索元素,类似于这里,谢谢HemChe,但不幸的是,我仍然有相同的问题-该方法在下拉列表展开后只获取前五个可见值,另一个的sysout是空的……也许JavascriptExecutor可以做到这一点,但我以前从未使用过它:-什么是dropdownValues.size重试?我只是想知道,所有隐藏元素是否也被包括在内;是第1天、第2天、第3天、第4天、第5天和第25天空行=System.out.printlndropdownValues.size总共31天;如果不是机密,请与我共享该网页,以便我可以调试该问题。