尝试打印链接名称时获取StaleElementReferenceException [英] Getting StaleElementReferenceException while trying print the link names
问题描述
我正在尝试打印谷歌搜索中显示的前5页链接..但是获取StateElementReferenceException不确定哪一个出错..
公共类GoogleCoInTest {
静态WebDriver驱动程序= null;
public static void main(String [] args)抛出InterruptedException {
System.setProperty(webdriver.gecko.driver,D:\\ balaup up \\personel \ \selenium\\Jars\\Drivers\\geckodriver.exe);
driver = new FirefoxDriver();
driver.manage()。window()。maximize();
driver.manage()。timeouts()。implicitlyWait(10,TimeUnit.SECONDS);
driver.get(https://www.google.co.in/);
//driver.findElement(By.xpath(\"//input[class='gsfi']\")).sendKeys(\"Banduchode);;
WebElement search = driver.findElement(By.cssSelector(input#lst-ib));
search.sendKeys(Banduchode);
search.sendKeys(Keys.ENTER);
printLinksName();
列表< WebElement> fiveLinks = driver.findElements(By.xpath( .//*[@ ID = NAV] / tbody的/ TR / TD /一个));
for(int i = 0; i< 5; i ++){
System.out.println(fiveLinks.get(i).getText());
fiveLinks.get(i).click();
Thread.sleep(5000);
printLinksName();
}
}
public static void printLinksName()throws InterruptedException {
List< WebElement> ; allLinks = driver.findElements(By.xpath(// * [@ id中= RSO] / DIV / DIV / DIV / DIV / DIV / H3 / A));
System.out.println(allLinks.size());
//打印所有列表
for(int i = 0; i< allLinks.size(); i ++){
System.out.println(Sno+( I + 1)+ : + allLinks.get(ⅰ).getText());
}
}
}
打印好到第二页,但在我得到之后
线程main中的异常org.openqa.selenium.StaleElementReferenceException: < a class =fl>的元素引用陈旧:元素不再附加到DOM或页面已刷新
有关此错误的文档,请访问:http://seleniumhq.org/exceptions/stale_element_reference.html
以下是您的问题的答案:
- 您的脚本按预期打印前2页的结果。
- 当您调用
printLinksName()时/ code>第一次有效。
- 接下来,您将10个PageNumbers存储在
通用列表
中输入WebElement
。 - 第一次在
for()
循环中你是单击WebElement
,然后通过调用<$打印所有链接c $ c> printLinksName()。 - 当您在
中进行第二次迭代时()
循环,List< WebElement>的引用由于DOM已更改,五个链接
将丢失。因此,您会看到StaleElementReferenceException
。
解决方案
避免 StaleElementReferenceException
的简单解决方案是移动代码行列表与LT; WebElement> fiveLinks = driver.findElements(By.xpath(.//* [@ id ='nav'] / tbody / tr / td / a));
with for()
循环。因此,您的代码块将如下所示:
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
公共类Q44970712_stale
{
静态WebDriver驱动程序= null;
public static void main(String [] args)throws InterruptedException
{
System.setProperty(webdriver.gecko.driver,C:\\Utility\\ \\\BrowserDrivers\\geckodriver.exe);
driver = new FirefoxDriver();
driver.manage()。window()。maximize();
driver.manage()。timeouts()。implicitlyWait(10,TimeUnit.SECONDS);
driver.get(https://www.google.co.in/);
//driver.findElement(By.xpath(\"//input[class='gsfi']\")).sendKeys(\"Banduchode);;
WebElement search = driver.findElement(By.cssSelector(input#lst-ib));
search.sendKeys(Banduchode);
search.sendKeys(Keys.ENTER);
printLinksName();
for(int i = 0; i< 5; i ++)
{
List< WebElement> fiveLinks = driver.findElements(By.xpath( .//*[@ ID = NAV] / tbody的/ TR / TD /一个));
System.out.println(fiveLinks.get(i).getText());
fiveLinks.get(i).click();
Thread.sleep(5000);
printLinksName();
}
}
public static void printLinksName()throws InterruptedException
{
List< WebElement> allLinks = driver.findElements(By.xpath(// * [@ id中= RSO] / DIV / DIV / DIV / DIV / DIV / H3 / A));
System.out.println(allLinks.size());
//打印所有列表
for(int i = 0; i< allLinks.size(); i ++)
{
System.out.println(斯诺 +(I + 1)+:+ allLinks.get(ⅰ).getText());
}
}
}
< blockquote>
注意:在这个简单的解决方案中,当你完成第二页的打印时,接下来你将创建 List< WebElement> fiveLinks
通过 xpath
与 .//* [@ id ='nav'] / tbody / tr / td / a
第二次, 第1页
是第一个存储在 fiveLinks <的元素/ code>列表。因此,您可能会再次被重定向到
Page 1
。为避免这种情况,您可以考虑使用正确的索引来获取 xpath
的帮助。
如果这回答你的问题,请告诉我。
I'm trying to print first 5 pages links displayed in google search.. But getting StateElementReferenceException Not sure which one went wrong..
public class GoogleCoInTest {
static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "D:\\bala back up\\personel\\selenium\\Jars\\Drivers\\geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.co.in/");
//driver.findElement(By.xpath("//input[class='gsfi']")).sendKeys("Banduchode");;
WebElement search=driver.findElement(By.cssSelector("input#lst-ib"));
search.sendKeys("Banduchode");
search.sendKeys(Keys.ENTER);
printLinksName();
List<WebElement> fiveLinks=driver.findElements(By.xpath(".//*[@id='nav']/tbody/tr/td/a"));
for(int i=0;i<5;i++){
System.out.println(fiveLinks.get(i).getText());
fiveLinks.get(i).click();
Thread.sleep(5000);
printLinksName();
}
}
public static void printLinksName() throws InterruptedException{
List<WebElement> allLinks=driver.findElements(By.xpath("//*[@id='rso']/div/div/div/div/div/h3/a"));
System.out.println(allLinks.size());
//print all list
for(int i=0;i<allLinks.size();i++){
System.out.println("Sno"+(i+1)+":"+allLinks.get(i).getText());
}
}
}
it prints fine till 2nd page , but there after I am getting
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: The element reference of <a class="fl"> stale: either the element is no longer attached to the DOM or the page has been refreshed
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Here is the Answer to your Question:
- Your script prints the result from the first 2 pages as expected.
- When you call
printLinksName()
for the first time it works. - Next, you are storing the 10 PageNumbers in a
Generic List
of typeWebElement
. - First time within the
for()
loop you are clicking on theWebElement
ofPage 2
and then printing all the links by callingprintLinksName()
. - While you are in the second iteration within
for()
loop, the reference ofList<WebElement> fiveLinks
is lost as the DOM have changed. Hence, you seeStaleElementReferenceException
.
Solution
A simple solution to avoid StaleElementReferenceException
would be to move the line of code List<WebElement> fiveLinks=driver.findElements(By.xpath(".//*[@id='nav']/tbody/tr/td/a"));
with in the for()
loop. So your code block will look like:
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Q44970712_stale
{
static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.co.in/");
//driver.findElement(By.xpath("//input[class='gsfi']")).sendKeys("Banduchode");;
WebElement search=driver.findElement(By.cssSelector("input#lst-ib"));
search.sendKeys("Banduchode");
search.sendKeys(Keys.ENTER);
printLinksName();
for(int i=0;i<5;i++)
{
List<WebElement> fiveLinks=driver.findElements(By.xpath(".//*[@id='nav']/tbody/tr/td/a"));
System.out.println(fiveLinks.get(i).getText());
fiveLinks.get(i).click();
Thread.sleep(5000);
printLinksName();
}
}
public static void printLinksName() throws InterruptedException
{
List<WebElement> allLinks=driver.findElements(By.xpath("//*[@id='rso']/div/div/div/div/div/h3/a"));
System.out.println(allLinks.size());
//print all list
for(int i=0;i<allLinks.size();i++)
{
System.out.println("Sno"+(i+1)+":"+allLinks.get(i).getText());
}
}
}
Note: In this simple solution when you finish printing the second page, next when you will create
List<WebElement> fiveLinks
throughxpath
with.//*[@id='nav']/tbody/tr/td/a
for second time,Page 1
is the first element which gets stored in thefiveLinks
List. Hence you may be again redirected toPage 1
. To avoid that you may consider to take help ofxpath
with proper indexing.
Let me know if this Answers your Question.
这篇关于尝试打印链接名称时获取StaleElementReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!