Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

c# - Selenium - stale element reference: element is not attached to the page

I am trying to understand why it gives an error while I am trying to display items that took from websites. I am also using google chrome for a browser.

chromeDriver.Navigate().GoToUrl("somewebsites");

chromeDriver.FindElement(By.Id("something.link.text")).Click();
chromeDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

And this other parts of my code

var item = chromeDriver.FindElementsByXPath("//*[starts-with(@id,"g_1_")]/td[3]/span");
foreach (var value in item)
{
    Console.WriteLine(value.Text); 
}

Whenever I use "chromeDriver.FindElement(By.Id("something.link.text")).Click();", it gives an error. I can't display the data that I pulled.

In the error message, It says "OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document". I checked the other posts, but I did not figure it out. How can I solve it?

As I read, selenium gives much faster response than website opening time, so it gives the error. Also, rarely, it gives that selenium cannot be located the element. Any suggestion?

EDIT: If I remove ".text" from display function, It displays "OpenQA.Selenium.Remote.RemoteWebElement". Does the problems with strings ?

EDIT2: I can hold data that I took from websites as like this:

[i]="System.Collections.ObjectModel.ReadOnlyCollection`1[OpenQA.Selenium.IWebElement]" For this, I wrote something like this:

string[] test= new string[100];

for (int i = 0; i < 100; i++)
{
     kaan[i] = driver.FindElements(By.XPath("//*[starts-with(@id,"g_1_")]/td[3]/span")).ToString();
}

How can I convert to the text

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I haven't worked in c# but have worked on java/selenium. But,I can give you the idea to overcome staleness.

Generally we will be getting the Stale Exception if the element attributes or something is changed after initiating the webelement. For example, in some cases if user tries to click on the same element on the same page but after page refresh, gets staleelement exception.

To overcome this, we can create the fresh webelement in case if the page is changed or refreshed. Below code can give you some idea.(It's in java but the concept will be same)

Example:

 webElement element = driver.findElement(by.xpath("//*[@id='StackOverflow']"));
 element.click();
 //page is refreshed
 element.click();//This will obviously throw stale exception

To overcome this, we can store the xpath in some string and use it create a fresh webelement as we go.

String xpath = "//*[@id='StackOverflow']";
driver.findElement(by.xpath(xpath)).click();
//page has been refreshed. Now create a new element and work on it
driver.findElement(by.xpath(xpath)).click();   //This works

In this case, we are collecting a group of webelements and iterating to get the text. But it seems there is some changes in the webelement after collecting the webelements and gettext throws staleness. We can use a loop and create the element on the go and get text.

for(int i = 0; i<5; i++)
{
   String value = driver.findElement(by.xpath("//.....["+i+"]")).getText);
   System.out.println(value);
}

Hope this helps you. Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...