java - org.openqa.selenium.ElementNotVisibleException

标签 java selenium selenium-webdriver

我正在尝试使用我正在使用的 Selenium 进行黑盒测试:

  • Mac 操作系统:版本:10.9.4
  • java.版本:1.6.0_65
  • 浏览器:firefox 版本 32.0.3
  • Selenium :版本 2.43.0

页面包含一个表,每一行都有一个唯一的ID,该ID被正确提取并存储在String itemId中 然后我创建相应的 WebElement 并尝试单击它,如下所示:

WebElement anchor = webDriver.findElement(By.id(itemId));
if (anchor != null) {           
    anchor.click();
    Sleeper.sleep(2);
}

我遇到了这个异常:

WARNING: Error extracting item data
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

我读过几个关于 WebDriverWait 的问题,并尝试了以下代码:

String itemId = item.getAttribute("id");
WebDriverWait wait = new WebDriverWait(webDriver, 10);           
WebElement anchor = wait.until(ExpectedConditions.elementToBeClickable(By.id(itemId)));
anchor.click();
Sleeper.sleep(2); // yes I know I waited 10 seconds already I have time :)

现在我得到以下异常:

WARNING: Error extracting item data
org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for element to be clickable

我尝试使用 ElementIsVisible 代替 elementToBeClickable,但它似乎已贬值

最佳答案

注意:此解决方案并不适用于出现类似错误的所有地方,但根据我的经验,这是 Web 应用程序上的常见问题。

如果您正在处理不可见的克隆表,您可以使用直接 xpath 到可见表或使用 findByELements 并在列表上迭代。

List<WebElement> temp_list = driver.findElements(By.id(itemId));
temp_list[1].click(); //example number, you have to find index corresponding to your element

请注意,只有当您有多个具有相同定位器的元素时,它才会起作用。您可以检查列表的大小。

temp_list.size();

关于java - org.openqa.selenium.ElementNotVisibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26424146/

相关文章:

html - 查找 <em> 标签中的元素 selenium

html - 无法使用 Selenium 在网页中定位元素(没有ID)

java - 如何在 web 服务中运行线程

java - 在 Java 中处理大数字

java - onclick javascript 函数需要 "turn on and off"div 属性以进行后续点击

python - 在几次成功的 Selenium Geckodriver 任务后连接被拒绝(操作系统错误 111)(Python)

java - 按钮 OnClick 开关不起作用

selenium - 如何在 Intern/Leadfoot(不是浏览器/客户端)中轮询条件?

java - 从 Selenium 调用 Tor 的 WebDriver

java - 如何使用 Selenium 和 Java 识别 <a> 中的文本