Selenium - 陈旧元素引用:元素未附加到页面上 [英] Selenium - stale element reference: element is not attached to the page

查看:1184
本文介绍了Selenium - 陈旧元素引用:元素未附加到页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么当我试图显示从网站上获取的项目时出现错误。
我也在浏览器中使用google chrome。

  chromeDriver.Navigate()。GoToUrl(somewebsites) ; 

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

和我的代码的其他部分

  var item = chromeDriver.FindElementsByXPath(// * [starts-with(@ id,\g_1_\)] / td [3] / span); 
foreach(项目中的var值)
{
Console.WriteLine(value.Text);
}

每当我使用 chromeDriver.FindElement(By.Id( something.link.text))。Click();,它给出一个错误。我无法显示我拉出的数据。



在错误消息中,它表示 OpenQA.Selenium.StaleElementReferenceException:'陈旧元素引用:元素未附加到页面文档 。我检查了其他帖子,但我没有弄明白。我该如何解决它?

据我所知,硒的反应比网站开放时间快得多,所以它会给出错误。此外,很少,它给硒不能位于元素。任何建议?



编辑:如果我从显示功能中删除 .text ,它会显示 OpenQA.Selenium.Remote。 RemoteWebElement 。是否有字符串的问题?



编辑2:我可以保存我从网站上获取的数据,如下所示:



[i] =System.Collections.ObjectModel.ReadOnlyCollection`1 [OpenQA.Selenium.IWebElement]
为此,我写了这样的内容:

  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] /跨度))的ToString();
}

如何转换为文本

解决方案

我没有在c#中工作,但已经在java / selenium上工作。但是,我可以给你提供克服过时的想法。



一般来说,如果在启动webelement后元素属性或某些内容发生更改,我们将会得到Stale Exception。例如,在某些情况下,如果用户尝试点击同一页面上的同一个元素,但在页面刷新后,则会得到staleelement异常。

为了解决这个问题,我们可以创建在页面被更改或刷新的情况下新的webelement。下面的代码可以给你一些想法(这是在java中,但概念将是相同的)



例子:

  webElement element = driver.findElement(by.xpath(// * [@ id ='StackOverflow'])); 
element.click();
//页面被刷新
element.click(); //这将显然抛出过时的异常

为了克服这个问题,我们可以将xpath存储在一些字符串中,并使用它创建一个新的webelement。

  String xpath =// * [@ id ='StackOverflow']; 
driver.findElement(by.xpath(xpath))。click();
//页面已被刷新。现在创建一个新元素并在其上进行操作
driver.fineElement(by.xpath(xpath))。click(); //这是可行的

在这种情况下,我们正在收集一组webelements并迭代获取文本。但是收集webelements和gettext引发过时之后,webelement似乎有了一些变化。

  for(int i = 0; i <5)我们可以使用循环并在旅途中创建元素并获取文本。 ; i ++)
{
String value = driver.findElement(by.xpath(//.....[\"+ i +]))。getText);
System.out.println(value);
}

希望这对您有所帮助。谢谢。


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

解决方案

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.fineElement(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.

这篇关于Selenium - 陈旧元素引用:元素未附加到页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆