gpt4 book ai didi

java - WebDriver 无法从 JQuery 下拉列表中选择

转载 作者:行者123 更新时间:2023-12-01 02:44:14 25 4
gpt4 key购买 nike

我有一个带有 JQuery 下拉菜单的 Web 表单。该特定字段包含出生日期。该字段的来源是:

<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();

}

该代码工作正常,但有一个异常(exception):它无法获取所有字段的值,只能获取打开下拉列表时第一个可见的字段的值。

1 2 3 4 5

问题是如何获取其他字段的值?

最佳答案

我要感谢你们的帮助(尤其是 HemChe)。

事实证明,这是最新的 FirefoxDriver 2.32 版本中的一个错误。相同的代码在 ChromeDriver 上运行得很好,并且我已经获得了所有 dropdown 值。将 selenium 版本降级到 2.31 解决了该问题,并且代码适用于两个驱动程序!

我将在 Selenium 错误跟踪器上注册一个错误!

这是我的代码的最终版本:

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();

}

干杯!

关于java - WebDriver 无法从 JQuery 下拉列表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16080869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com