0

我正在使用 Selenium 2/WebDriver 进行自动化。我有一张桌子,正在尝试在其中选择一行。当测试运行时,我可以看到该行变得突出显示,就好像它被单击了一样,但是我立即得到:

“org.openqa.selenium.StaleElementReferenceException:元素不再附加到 DOM”

错误并且测试失败。

代码如下:

@Test
public void rowSelection() throws Exception
{
    SeleniumHelper helper = new SeleniumHelper();
    action = new SeleniumActionHelper(driver);

    helper.login();

    String testUrl = navigateToUrl("option/listOptions.xhtml");
    driver.get(testUrl);

    WebElement table = findElementById("tableSection:dataTableWrapped_data");
    List<WebElement> allRows = table.findElements(By.tagName("tr"));
    for (WebElement row : allRows)
        {
            List<WebElement> cells = row.findElements(By.tagName("td"));
            for (WebElement cell : cells)
            {
                WebElement listName = cell.findElement(By.xpath("./*[text()='body_build']"));
                listName.click();
            }
        }
}

我在 listName.click() 操作之前和之后放置了一个 Thread.sleep(2000) ,但都没有帮助。任何帮助将不胜感激。

4

1 回答 1

0

如果您认为这是一个同步问题,您可以使用wait.until

WebElement webElement = wait.until(ExpectedConditions.visibilityOf(listName));

webElement.click();

如果在您执行单击操作时定位器发生更改,请设置一个断点并检查您的应用程序。如果是这样,您必须考虑更好的定位器。

于 2016-08-30T14:31:14.957 回答