使用Selenium导航时的陈旧对象参考 [英] Stale Object Reference while Navigation using Selenium

查看:106
本文介绍了使用Selenium导航时的陈旧对象参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试一个简单的程序,该程序可以导航并从新页面获取数据,返回历史记录并打开其他页面并获取数据,依此类推,直到访问了所有链接并获取了数据.

I have been trying a simple program that navigates and fetches data from the new page, comes back in history and open other page and fetch data and so on until all the links have been visited and data is fetched.

在下面的网站上获得结果后,我试图遍历第一栏中获得的所有链接,并逐个打开这些链接,并从每个页面中提取文本.但是下面的程序仅访问第一个链接并给出StaleElementReferenceException,我尝试使用Actions,但是它没有用,并且我不了解JavascriptExecutor.我还尝试了在其他SO问题上发布的解决方案,其中一个是在此处.我想在下面的代码和有效的代码中更正错误.

After getting results on the below site, i am trying to loop through all the links i get in the first column and open those links one by one and extract text from each of these page. But the below program only visits first link and gives StaleElementReferenceException, I have tried using Actions but it didn't work and I am not aware about JavascriptExecutor. I also tried solutions posted on other SO questions, one of which was mine over here. I would like to have the mistake corrected in the below code and a working code.

public class Selenium {

    private final static String CHROME_DRIVER = "C:\\Selenium\\chromedriver\\chromedriver.exe";
    private static WebDriver driver = null;
    private static WebDriverWait wait = null;

    private void setConnection() {
        try {
            System.setProperty("webdriver.chrome.driver", CHROME_DRIVER);
            driver = ChromeDriver.class.newInstance();
            wait = new WebDriverWait(driver, 5);
            driver.get("https://sanctionssearch.ofac.treas.gov");
            this.search();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void search() {
        try {
            driver.findElement(By.id("ctl00_MainContent_txtLastName")).sendKeys("Dawood");
            driver.findElement(By.id("ctl00_MainContent_btnSearch")).click();
            this.extractText();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void extractText() {
        try {
            List<WebElement> rows = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr"));
            List<WebElement> links = null;
            for (int i = 1; i <= rows.size(); i++) {

                links = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a"));

                for (int j = 0; j < links.size(); j++) {
                    System.out.println(links.get(j).getText() + ", ");
                    links.get(j).click();
                    System.out.println("Afte click");
                    driver.findElement(By.id("ctl00_MainContent_btnBack")).click();
                    this.search();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] ar) {
        Selenium object = new Selenium();
        object.setConnection();
    }

}

推荐答案

请检查此代码

   private void extractText() {
        try {
            List<WebElement> rows = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr"));
            List<WebElement> links = null;
            System.out.println(rows.size());
            for (int i = 0; i < rows.size(); i++) {
                links = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a"));
                WebElement ele= links.get(0);
                System.out.println(ele.getText() + ", ");
                ele.click();
                System.out.println("After click");
                driver.findElement(By.id("ctl00_MainContent_btnBack")).click();                     
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这篇关于使用Selenium导航时的陈旧对象参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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