c# - selenium : stale element reference

151 views
Skip to first unread message

Nortier David

unread,
Jan 12, 2023, 5:44:58 AM1/12/23
to seleniu...@googlegroups.com

Yo girl & man,

 

I’ve a problem (Youston).

 

I find a element, with method : Driver.FindElement(By.XPath(…)
 

Next line, I try: element.click()

 

  • Error :   stale element reference: element is not attached to the page document (Session info: chrome=109.0.5414.74)
    org.openqa.selenium.StaleElementReferenceException

 

Yet the element is still there (in debug I see it)

 

I’ve read some helps on the web, but I don’t understand the problem.

And I don’t know how to solve this problem

 

 

Many many thanks

 

 

  David

 

 


====== DISCLAIMER ======

https://www.cph.be/maildisclaimer

Vinod Tanguturi

unread,
Jan 12, 2023, 8:47:38 AM1/12/23
to seleniu...@googlegroups.com
The stale element refers that the Dom has refreshed after you identified the element and before clicking that element. You need to find that element again before the click event and also use a wait statement just in case the page takes time to load.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/217d70c44eb84ebe8a555afdd198de09%40cph.be.

Nortier David

unread,
Jan 13, 2023, 3:58:38 AM1/13/23
to seleniu...@googlegroups.com

Thanks for response

 

If I scroll top this element before to click it, is the DOM refreshed ?

 

De : seleniu...@googlegroups.com <seleniu...@googlegroups.com> De la part de Vinod Tanguturi
Envoyé : jeudi 12 janvier 2023 14:47
À : seleniu...@googlegroups.com
Objet : Re: [selenium-users] c# - selenium : stale element reference

Chandrika sai

unread,
Jan 13, 2023, 4:28:46 AM1/13/23
to seleniu...@googlegroups.com
Hi...

The element may be inside a frame, you cannot see that frame on web page. 
so once again you identify that element properly, may be it will be inside that frame.

so in the code, you write like, first you need to switch to that frame, and then write locator
to that element, and then you need to click on that element.

Thank you
Chandrika

ebubekir bastama

unread,
Jan 13, 2023, 6:50:50 AM1/13/23
to seleniu...@googlegroups.com
I can assist you with C# Selenium in ı am Discord channel live.
https://discord.gg/XbrQyqQw

Chandrika sai <chandrik...@gmail.com>, 13 Oca 2023 Cum, 12:28 tarihinde şunu yazdı:

Anuv4123

unread,
Jan 13, 2023, 8:38:44 AM1/13/23
to Selenium-Users
Not sure how your application was developed. You spy the element and try to scroll down and up. If the source in the inspect element screen has any reload happens then it means your dom got refreshed. Just to be safe try to find element before click and see if that works 

Thanks



Hamim Alam

unread,
Jan 15, 2023, 11:17:35 PM1/15/23
to Selenium Users
Usually in this kind of situation you might want use something like retry and click. I have written below function in JAVA which you can take reference of.

```
public static boolean retryingFindClick(WebDriver driver, String ele) {
        boolean result = false;
        int attempts = 0;
        while(attempts < 2) {
            try {
                driver.findElement(By.xapth(ele)).click();
                result = true;
                break;
            } catch(NoSuchElementException e) {
            }
            attempts++;
        }
        return result;
    }
```

Hope this helps!!!

Ayush Yadav

unread,
Jan 15, 2023, 11:17:35 PM1/15/23
to Selenium Users
Hey man, 
You can also use waits in selenium and make use of Presence of element detected function.
For example in python, use:
  WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".reply-button"))).click()
Reply all
Reply to author
Forward
0 new messages