我正在尝试使用以下代码从下拉菜单中获取所有元素:
List<WebElement> actmenu = driver.findElements(By.className("mbrMenuItems"));
for (int i = 0; i < actmenu.size(); i++) {
System.out.println(actmenu.get(i).getText());
}
actmenu.get(0).click();
actmenu.get(1).click();
所以,我能够使用for循环打印链接文本,但我无法点击元素,抛出以下错误:
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.31 seconds
解决此错误的任何帮助?
答案 0 :(得分:1)
方法.getText()
从HTML源返回值。也许您要点击的元素尚未呈现?尝试等待元素
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(element);
以上代码将等待10秒,直到元素可见。