有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java组织。openqa。硒。ElementNotVisibleException

我正在尝试使用selenium进行黑盒测试,我正在使用:

  • 苹果操作系统:版本:10.9.4
  • 爪哇。版本: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代替element,但它似乎没有什么吸引力


共 (1) 个答案

  1. # 1 楼答案

    注意:这种解决方案不适用于任何发生类似错误的地方,但根据我的经验,这是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();