• Register
0 votes

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)));
catch(org.openqa.selenium.StaleElementReferenceException ex)
WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));

See if above workaround can help you !


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


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.


 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 18 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 14 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 14 views
0 votes
1 answer 9 views
1 vote
1 answer 1.5K views
Problem: Hello guys, I have a chunk of python code where I am trying to extract all the links from a particular website. For that, I am using selenium.webdriver. But the program is not retrieving the links from that website as I am expecting. Instead of that, it is ... problem? I really appreciate your help in this regard. It took already much times from me but couldn't find any way. Thanks a lot.
asked Apr 21, 2020 Gavin 15.3k points
1 vote
1 answer 864 views
Problem : I am not completely expert in Python, but still I am willing to learn good practices while I develop my skills. I am trying to remove the high and low values from the list of numbers, I know how to do this , but I want to know the better way to do this. myrawlist = [ ... 2, 3, 4] mytrimmedlist = mylist[1:-1] #[1, 2, 3] I get the required output, but looking for the better way to do this.
asked Dec 14, 2019 alecxe 7.5k points
0 votes
1 answer 116 views
Problem : I am trying to learn the Selenium by myself. But now my Sendkeys is not working. Can anybody please help me to fix my issue. My code is as below: WebElement uname = driver.findElement(By.xpath("//*[@id='username']"));      uname.sendKeys("123456"); Below is the error: Exception in ... ip: '', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_161'
asked Feb 14, 2020 mphil 2.3k points
0 votes
1 answer 1.1K views
Problem : I have come across a below run-time error from the JPA 2.0 program I think it is related to the Firefox Profile for some reason: Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/Maps at org.openqa.selenium.firefox. ... it is supported by my Selenium Webdriver but I suspect that above issue is to do with not able to read the profile directory.
asked Feb 3, 2020 jwilliam 3.9k points
1 vote
1 answer 43 views
Problem: I was trying to check if an element does exist in a list using a pyhton program: primary_list = [ 10, 12, 16, 6, 20, 33 ] a = input("Please enter a number to check if this exists in the list: ") print("Checking if input exists in this list : ") for ... not giving any output that is right. What should I do to fix this and how to check if an element does exist in a list. Thanks in advance.
asked May 16, 2020 samhaz 5k points
0 votes
2 answers 198 views
Problem : Facing following Selenium Webdriver error the path to the driver executable must be set by the webdriver.chrome.driver system property
asked Nov 15, 2019 peterlaw 6.9k points