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);
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • which line raises the error ? – Ji aSH Jan 23 '18 at 10:14
  • You need to wait after switching between the pages – Ankur Singh Jan 23 '18 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. – user8537453 Jan 23 '18 at 10:25
  • Possible duplicate of [Getting StaleElementReferenceException while trying print the link names](https://stackoverflow.com/questions/44970712/getting-staleelementreferenceexception-while-trying-print-the-link-names) – undetected Selenium Jan 23 '18 at 10:32
  • This code concerning the page. http://www.domiporta.pl/mieszkanie/sprzedam?Localization=dolno%C5%9Bl%C4%85skie&PageNumber=21&SortingOrder=InsertionDate. 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. – Tomek Nowakowski Jan 23 '18 at 14:30

1 Answers1

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);
}
Ji aSH
  • 3,206
  • 1
  • 10
  • 18
  • 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 = "http://www.domiporta.pl/mieszkanie/sprzedam?Localization=dolno%C5%9Bl%C4%85skie&PageNumber=22&SortingOrder=InsertionDate"; – Tomek Nowakowski Jan 23 '18 at 14:54
  • Part 2// łącze się z Domiporta driver.get(startPage); List rows = driver.findElements(By.className("detail-card__heading")); List cols=new ArrayList(); for(int i=0;i – Tomek Nowakowski Jan 23 '18 at 14:55
  • 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"); } } – Tomek Nowakowski Jan 23 '18 at 14:56
  • 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. – Tomek Nowakowski Jan 23 '18 at 15:02