0

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.

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();
    }

}
2
  • Can't you just do a new ChromeDriver() instead of ChromeDriver.class.newInstance()? What's this reflection for? Commented Aug 1, 2017 at 9:52
  • There's no specific reason i did that, can you please point the mistake due to which i am getting the Stale element exception? Commented Aug 1, 2017 at 10:21

3 Answers 3

0

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.

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you correct the mistake in my program and paste the answer? As you had already replied on my previous question and i have tried this solution!! Thanks!
Instead of links --> construct a fresh webelement and click -->driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr[+i+]/td[1]/a")).click();
for (int i = 1; i <= rows.size(); i++) { links = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a")); for (int j = 1; j < links.size(); j++) { WebElement m = driver.findElement(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a")); m.click(); System.out.println("Afte click"); driver.findElement(By.id("ctl00_MainContent_btnBack")).click(); this.search(); } } But it still doesn't works :(
in the xpath, we need to parameterize "//*[@id='gvSearchResults']/tbod‌​y/tr[1]/td[1]/a" --> tr[1]., tr[2] and so on
Can u pls post the modified block of code here? I have tried nearly everything and not able to get it....it would be really appreciated if you do!
|
0

The reason you get StaleElementReference Exception, is normally because you stored element(s) into some variable, however after that you did some action and page has changed (due to some ajax response) and so your stored element has become stale.

The best solution is not to store element in any variable in such case.

This should work.

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() + ", ");
    driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a")).get(j).click();
    System.out.println("Afte click");
    driver.findElement(By.id("ctl00_MainContent_btnBack")).click();
    this.search();
}

5 Comments

Even this gives Stale Element Exception :(
how about this driver.findElement(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td["+j+"]/a")).click(). also try putting some wait before you get this element if you get this exception again.
where are you getting this exception, i mean at which line?
after trying your code it is giving error at backbutton.click()
That means your browser did not properly reload the page yet. You will have to wait until loading is complete before searching and clicking buttons.
0

Please check this code

   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();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.