-2

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\1\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)

2 Answers 2

0

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.

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.

2
  • Thanks for he reply, I took out that bit, but it just changed the error: Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified (Session info: chrome=39.0.2171.95)
    – Trish H
    Commented Dec 18, 2014 at 15:21
  • 1
    I think the cssSelector is wrong here. Just replace css=button.btn.btn-default with only button.btn.btn-default as is the classname of the button I presume. Adding css is not the way to define a cssSelector.I have edited my code above accordingly. Please check if that works out for you.
    – Subh
    Commented Dec 18, 2014 at 15:44
-4

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;
        }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.