0

I am clicking on the text box displayed on first page

WebElement txtBox = driver.findElement(By.xpath("---xpath---"));
      txtBox.click();

Then after some block of execution I am getting the same textbox in new page on same window. Here also I want to click on the text box. I used JavascriptExecutor to do this scripting.

((JavascriptExecutor)driver).executeScript("arguments[0].click();", txtBox );

But while running the script I am getting an error message saying:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

2 Answers 2

1

You need to execute driver.findElement() every time you reload the page.

This method returns WebElement which is integral part of current page. If you refresh browser or navigate to some other URL or even if element is deleted and attached again by some javascript on same page, previously found element cannot be used anymore.

Here you have official explanation of this exception: http://www.seleniumhq.org/exceptions/stale_element_reference.jsp

Sign up to request clarification or add additional context in comments.

Comments

0

Stale Element exception comes after you want to interact with an element loaded previously. If you get webelement and then reload the page it gives this exception because it is not in a newly created page. It is better to click the web element without assigning it to a variable like:

driver.findElement(By.xpath("---xpath---")).click();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.