尝试打印链接名称时获取StaleElementReferenceException [英] Getting StaleElementReferenceException while trying print the link names

查看:93
本文介绍了尝试打印链接名称时获取StaleElementReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印谷歌搜索中显示的前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


解决方案

以下是您的问题的答案:


  1. 您的脚本按预期打印前2页的结果。

  2. 当您调用 printLinksName()时/ code>第一次有效。

  3. 接下来,您将10个PageNumbers存储在通用列表中输入 WebElement

  4. 第一次在 for()循环中你是单击 WebElement ,然后通过调用<$打印所有链接c $ c> printLinksName()

  5. 当您在中进行第二次迭代时()循环, 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:

  1. Your script prints the result from the first 2 pages as expected.
  2. When you call printLinksName() for the first time it works.
  3. Next, you are storing the 10 PageNumbers in a Generic List of type WebElement.
  4. First time within the for() loop you are clicking on the WebElement of Page 2 and then printing all the links by calling printLinksName().
  5. While you are in the second iteration within for() loop, the reference of List<WebElement> fiveLinks is lost as the DOM have changed. Hence, you see StaleElementReferenceException.

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 through xpath with .//*[@id='nav']/tbody/tr/td/a for second time, Page 1 is the first element which gets stored in the fiveLinks List. Hence you may be again redirected to Page 1. To avoid that you may consider to take help of xpath with proper indexing.

Let me know if this Answers your Question.

这篇关于尝试打印链接名称时获取StaleElementReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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