如何解决无法使用Webdriver中的Actions类从下拉菜单中选择元素
我正在尝试从下拉列表中选择一个元素。我没有选择使用Select类的选择,并且尝试使用Actions类。它仅定位下拉列表,但由于无法识别xpath,因此未选择下拉列表中的元素。 xpath似乎是正确的,但仍未被接受。下面是HTML开发工具和Java中的代码。
<a id="6008" name="mItem" nowrap="true" class="itemStyle" style="color: black; background-color: rgb(238,243,251); cursor: default;"><div class="arrow-right"></div>Financial Planning</a>
<a id="1693" name="mItem" nowrap="true" class="itemStyle" style="color: black; background-color: rgb(238,251); cursor: default;"><div class="arrow-right"></div>Assets/Revenue</a>
<a id="30241" name="mItem" nowrap="true" class="itemStyle" style="cursor: pointer; color: white; background-color: rgb(127,158,195);">FA Productivity</a>
Java代码:
WebElement wb = webDriver.findElement(By.xpath("//*[@id='1692']"));
Thread.sleep(2000);
Actions mouse = new Actions(webDriver);
mouse.moveToElement(wb).click();
WebElement wb2 = webDriver.findElement(By.xpath("//*[@id='30241']"));
Thread.sleep(2000);
mouse.moveToElement(wb2).click();
mouse.build();
mouse.perform();
解决方法
如果您的目标是仅选择元素,则可以使用javascript click,只要元素存在于DOM中,它就会单击该元素,这与需要显示元素的常规硒单击不同
Driver.get().executeScript("arguments[0].click();",element);
请尝试以下代码。
List<WebElement> options = driver.findElements(By.xpath(""));
for(WebElement option : options) {
if (option.getText().contains("FA Productivity")) {
option.click();
break;
}
}
请尽可能共享测试网址,以便我复制您的方案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。