0

I am trying to write such applications in selenium. Enters the subpage, gets data, goes back, enters the next subpage ... Unfortunately, an exception appears to me

"org.openqa.selenium.StaleElementReferenceException: Element is no longer valid"

all right - after reloading it's another page. Any ideas?

Code:

List<WebElement> rows = driver.findElements(By.className("detail-card__heading"));
List<WebElement> cols=new ArrayList<WebElement>();
for(int i=0;i<rows.size();i++){
System.out.println("Nr oferty: "+i);
cols=rows.get(i).findElements(By.tagName("div"));
for(WebElement col:cols) {
System.out.print("cell value "+col.getText());
 col.click();
}
 driver.get(CurrentUrl);
}
5
  • which line raises the error ? Commented Jan 23, 2018 at 10:14
  • You need to wait after switching between the pages Commented Jan 23, 2018 at 10:18
  • What do you mean by subpage? Are you trying to move through a data table? BTW Ankur is right, it takes a few seconds for the page to load and the DOM to be populated. Also depending on what col.click triggers, it could cause time out issues as well if the page hasn't finished updating when it hits the next start of the loop. You could try throwing in a few waits or or subloops looking for element != null. Without more details, it's difficult to be more helpful. Commented Jan 23, 2018 at 10:25
  • Possible duplicate of Getting StaleElementReferenceException while trying print the link names Commented Jan 23, 2018 at 10:32
  • This code concerning the page. domiporta.pl/mieszkanie/…. This is a list of estatetype offers. The problem is col.click(); When I cancel this line everything is OK. But this fragment of the code is the most important. col.click() is entering to the offer(subpage) after taking some data from the offer the program should go back to the list and enter to the next offer. But in this moment the problem appears - page is no longer valid. Commented Jan 23, 2018 at 14:30

1 Answer 1

0

Ok I got it.

You have to understand that when you use 'findElement', selenium stores a direct reference to the corresponding DOM element. It does not store the 'By' condition.

What it means is each time you reload the page with 'get(url)', you will loose all the actual selenium elements since the whole html page is rerendered. In such cases selenium raises a 'stale element' exception meaning that the referenced DOM element is no longer present in the DOM.

To avoid this error you will have to re-find your 'rows' element on every iteration

List<WebElement> rows = driver.findElements(By.className("detail-card__heading"));
List<WebElement> cols=new ArrayList<WebElement>();
for(int i=0;i<rows.size();i++){
    System.out.println("Nr oferty: "+i);
    rows = driver.findElements(By.className("detail-card__heading"));
    cols=rows.get(i).findElements(By.tagName("div"));
    for(WebElement col:cols) {
        System.out.print("cell value "+col.getText());
        col.click();
    }
    driver.get(CurrentUrl);
}
Sign up to request clarification or add additional context in comments.

4 Comments

import java.util.ArrayList; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; public class Domiporta{ public static void main(String[] args) throws InterruptedException { //Zmienne ogólne //Dane dla dolnośląskie.pi String startPage = "domiporta.pl/mieszkanie/…";
Part 2// łącze się z Domiporta driver.get(startPage); List<WebElement> rows = driver.findElements(By.className("detail-card__heading")); List<WebElement> cols=new ArrayList<WebElement>(); for(int i=0;i<rows.size();i++){ try { System.out.println("Nr oferty: "+i); cols=rows.get(i).findElements(By.tagName("div")); for(WebElement col:cols) { System.out.print("cell value "+col.getText()); //With click doesn't working //col.click(); //driver.get(startPage); System.out.println("Przeszła oferta numer"+i++); }
System.out.println("it was offer no "+i++); }catch(org.openqa.selenium.StaleElementReferenceException ex) { System.out.println("Poszedł błąd " +i); } } System.out.println("Koniec"); } }
as user8537453 and @Ash as You can see in this way code working, I can list all list. Problem is when I want to click to the offer (end of second part of the code //col.click(); I'd like to go into each offer, take some data and go back but when I enter to the offer and fo back driver.get(startPage) is error.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.