无法使用Webdriver中的Actions类从下拉菜单中选择元素

问题描述

我正在尝试从下拉列表中选择一个元素。我没有选择使用Select类的选择,并且尝试使用Actions类。它仅定位下拉列表,但由于无法识别xpath,因此未选择下拉列表中的元素。 xpath似乎是正确的,但仍未被接受。下面是HTML开发工具和Java中的代码

HTML code

<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;
   }
 }

请尽可能共享测试网址,以便我复制您的方案。