• Register
0 votes
1.7k views

Problem :

I have the list which has the multiple links under each section. Each section has the same links I need to click the particular link under each section. I have written my code for it but when I execute the code it gives me below error:

Exception in thread "main" 
org.openqa.selenium.StaleElementReferenceException: stale element 
reference: element is not attached to the page document

 

7 5 2
3,870 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

A reason for this error is because a element to which you have referred is removed from a DOM structure

I was facing a same problem while working with IEDriver. A reason was because javascript loaded my element one more time after I have referred so my date reference pointed to the unexisting object even if it was right their on the UI. I used a below workaround.

try {
WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
date.click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
date.click();
}

See if above workaround can help you !

OR

The thing here is that you are trying to use a for loop outside of the conditional statement.

After a conditions in your IF statement are met you probably navigate to the another page, so when a for loop attempts to iterate once more, then you get a stale element error because now you are on the different page.

You can add the break at the end of the if statement, this really worked for me.

9 7 4
38,600 points
0 votes

Solution:

In this article we will learn how to avoid state element reference problems. Sometimes this may happen for the wrong path directory from input. We experiment with some of the code methods that may be fit for the environment or system you. Although there is no straightforward solution for something like this, since it is a content specific problem.

Reason:

 We can assume your calling function has not been deleted as you can see it.

So we are managing with the second case. This usually means that the page is reloading in between you locating the element in the server. Hence your interaction with the element. To debug this - put a breakpoint on the

Error occurs for example when you have a web element reference and the document it was retrieved from navigates. After navigation, all web element references to the previous document will be discarded along with the document. This will cause any ambiguous coding with the web element to fail with the stale element reference error.

Finally we overcome the static problem by some input activity the keyword dropdown manual view. There we will make a script that fetches the document.

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 8 views
8 views
Problem: Why this message keep shown: Message: stale element reference: element is not attached to the page document
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 11 views
11 views
Problem: I have a list that has multiple links under each section. Each section has some links I need to click a particular link under each section. I have the code but when it executes it gives me: stale element reference: element is not attached to the page document error.
asked Mar 23 ummesalma 25.2k points
0 votes
1 answer 3 views
3 views
Problem: Trying with parsing but in vain . Xml parsing error junk after document element.
asked Mar 23 PkGuy 13.1k points
0 votes
1 answer 5 views
5 views
Problem: Do any one know hoew to fix this ? please ensure the message is rfc 5322 compliant
asked Mar 19 PkGuy 13.1k points
0 votes
1 answer 2 views
2 views
Problem: I have been debugging this for days and I can't seem to know why the error keeps telling me that The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document. What could be the problem?
asked Apr 8 Sana8989 8.9k points
0 votes
1 answer 3 views
3 views
Problem; I need someone to understand this problem and find out the solution. difference between page object model and page factory
asked Apr 4 Ifra 24.4k points
0 votes
1 answer 23 views
23 views
Problem: I am very new to Selenium and Java both. I am trying to select and get the dropdown value in a Selenium WebDriver using the Java. The HTML Code is as below: <select class="form_input_select bx-def-font" name="Sex[0]"> <option value="Male">Man</option> < ... it only shows _Others Kindly help me to solve above issue. I am unable to fix above issue after working on it for more than 3 days.
asked Jul 9, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 3 views
3 views
Problem: getwindowhandle(); -> Which is used for getting the parent window name. getwindowhandles(): -> which is used for getting the all the child windows link . but how to use with string & iterator concept ?
asked 4 days ago Atik03 10.1k points
0 votes
1 answer 18 views
18 views
I am finding a program that will help me to handle multiple windows in Selenium Webdriver.
asked Oct 5, 2020 Daniel Anderson 4k points
0 votes
1 answer 15 views