Java Selenium 错误:过时的元素参考:元素未附加到页面文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27534649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:32:11  来源:igfitidea点击:

Selenium ERROR : stale element reference: element is not attached to the page document

javaeclipseangularjsgoogle-chromeselenium

提问by Trish H

I keep getting this error, I am need to testing so it maybe my code but I am not sure.

我不断收到此错误,我需要进行测试,因此可能是我的代码,但我不确定。

Here is my code:

这是我的代码:

public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver", "C:\selenium-2.44.0\chromedriver.exe");
//  System.setProperty("webdriver.ie.driver", "C:\selenium-2.44.0\IEDriverServer.exe");

    WebDriver driver = new ChromeDriver();
//  WebDriver driver = new InternetExplorerDriver();
//  WebDriver driver = new FirefoxDriver();

    driver.get("https://multichannel-custom-qa.Support/");
    driver.findElement(By.id("userID")).sendKeys("REH01");
    driver.findElement(By.name("j_password")).sendKeys("YE02year");
    driver.findElement(By.name("action")).submit();
    String tagName = "";

    driver.findElement(By.linkText("Workbench")).click();

    WebElement searchBox;
    searchBox = driver.findElement(By.xpath("//input[@type='text']"));
    searchBox.sendKeys("X1508706");
    searchBox.findElement(By.cssSelector("css=button.btn.btn-default")).click();
    Thread.sleep(5000);

    searchBox.findElement(By.cssSelector("css=button.btn.btn-default"));
    searchBox.click();

    WebElement link;
    link = driver.findElement(By.linkText("X1508706"));
    link.click();
    Thread.sleep(5000);

And here is the error:

这是错误:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=39.0.2171.95)
  (Driver info: chromedriver=2.12.301325 (962dea43ddd90e7e4224a03fa3c36a421281abb7),platform=Windows NT 6.1 SP1 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 214 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'C9090DEF19ED258', ip: '172.22.46.117', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_20'
Session ID: e3c034f27280c888088da4ba7922cdee
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\kxh8178\AppData\Local\Temp\scoped_dir8948_22544}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=39.0.2171.95, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:89)
    at driverTest.tests.main(tests.java:33)

回答by Subh

I am not sure.. But, from your code, it seems you are trying to click the same element twice. And, probably the first click on the element results in a page refresh or something, resulting in DOM change, and hence in StaleElementReferenceException.

我不确定.. 但是,从您的代码来看,您似乎正在尝试两次单击同一个元素。并且,可能第一次单击元素会导致页面刷新或其他内容,从而导致 DOM 更改,从而导致StaleElementReferenceException

The below code snippet seems to be the culprit:

下面的代码片段似乎是罪魁祸首:

//1
searchBox.findElement(By.cssSelector("button.btn.btn-default")).click();

//2 
searchBox.findElement(By.cssSelector("button.btn.btn-default"));
searchBox.click();

As you can see, Both code "//1" and "//2" perform the same action on the element.If you remove any one of them, I think your problem will be solved.

如您所见,代码“//1”和“//2”对元素执行相同的操作。如果您删除其中任何一个,我认为您的问题将得到解决。

回答by Subh

use this code to wait till the element is attached:

使用此代码等待元素被附加:

boolean breakIt = true;
        while (true) {
        breakIt = true;
        try {
            // write your code here
        } catch (Exception e) {
            if (e.getMessage().contains("element is not attached")) {
                breakIt = false;
            }
        }
        if (breakIt) {
            break;
        }

    }