随机“元素不再附加到DOM”。 StaleElementReferenceException [英] Random "Element is no longer attached to the DOM" StaleElementReferenceException

查看:156
本文介绍了随机“元素不再附加到DOM”。 StaleElementReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这只是我,但Selenium Webdriver似乎是一场彻头彻尾的噩梦。 Chrome webdriver目前无法使用,而其他驱动程序则非常不可靠,或者看起来如此。我正在与许多问题作斗争,但这里有一个问题。

I'm hoping it's just me, but Selenium Webdriver seems like a complete nightmare. The Chrome webdriver is currently unusable, and the other drivers are quite unreliable, or so it seems. I am battling many problems, but here is one.

随机地,我的测试将失败,并且

Randomly, my tests will fail with a

"org.openqa.selenium.StaleElementReferenceException: Element is no longer attached 
to the DOM    
System info: os.name: 'Windows 7', os.arch: 'amd64',
 os.version: '6.1', java.version: '1.6.0_23'"

我正在使用webdriver版本2.0 B3。我已经看到FF和IE驱动程序发生这种情况。我可以阻止这种情况的唯一方法是在异常发生之前添加对 Thread.sleep 的实际调用。这是一个糟糕的解决方法,所以我希望有人可以指出我的错误,这将使这一切变得更好。

I'm using webdriver versions 2.0b3. I have seen this happen with FF and IE drivers. The only way I can prevent this is to add an actual call to Thread.sleep before the exception occurs. That is a poor workaround though, so I'm hoping someone can point out an error on my part that will make this all better.

推荐答案

是的,如果您遇到StaleElementReferenceExceptions问题,那是因为您的测试写得不好。这是一场竞争。请考虑以下情形:

Yes, if you're having problems with StaleElementReferenceExceptions it's because your tests are poorly written. It's a race condition. Consider the following scenario:

WebElement element = driver.findElement(By.id("foo"));
// DOM changes - page is refreshed, or element is removed and re-added
element.click();

现在,在您点击元素的位置,元素参考不再有效。 WebDriver几乎不可能对可能发生这种情况的所有情况做出很好的猜测 - 所以它会抛出手并控制你,因为测试/应用程序作者应该确切知道可能会发生什么或不会发生什么。您要做的是明确等待,直到DOM处于您不知道事情不会改变的状态。例如,使用WebDriverWait等待特定元素存在:

Now at the point where you're clicking the element, the element reference is no longer valid. It's close to impossible for WebDriver to make a good guess about all the cases where this might happen - so it throws up its hands and gives control to you, who as the test/app author should know exactly what may or may not happen. What you want to do is explicitly wait until the DOM is in a state where you know things won't change. For example, using a WebDriverWait to wait for a specific element to exist:

// times out after 5 seconds
WebDriverWait wait = new WebDriverWait(driver, 5);

// while the following loop runs, the DOM changes - 
// page is refreshed, or element is removed and re-added
wait.until(presenceOfElementLocated(By.id("container-element")));        

// now we're good - let's click the element
driver.findElement(By.id("foo")).click();

presenceOfElementLocated()方法如下所示:

The presenceOfElementLocated() method would look something like this:

private static Function<WebDriver,WebElement> presenceOfElementLocated(final By locator) {
    return new Function<WebDriver, WebElement>() {
        @Override
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    };
}

你对目前的Chrome驱动程序非常不稳定你是对的,而你很高兴听到Selenium后备箱有一个重写的Chrome驱动程序,其中大部分实现都是由Chromium开发人员完成的,作为他们树的一部分。

You're quite right about the current Chrome driver being quite unstable, and you'll be happy to hear that the Selenium trunk has a rewritten Chrome driver, where most of the implementation was done by the Chromium developers as part of their tree.

PS。或者,不要像上面的例子那样明确地等待,你可以启用隐式等待 - 这样WebDriver将一直循环直到指定的超时等待元素出现:

PS. Alternatively, instead of waiting explicitly like in the example above, you can enable implicit waits - this way WebDriver will always loop up until the specified timeout waiting for the element to become present:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)

根据我的经验,明确等待总是更可靠。

In my experience though, explicitly waiting is always more reliable.

这篇关于随机“元素不再附加到DOM”。 StaleElementReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆