Skip to content
Advertisement

Stale element: iterating through webelement list python

I’m using selenium for the first time to get some information about a fantasy soccer game I play with my friends (we have a competition). I’m facing issues iterating through a list of webelements. Apparently they become stale. Here’s some code and details:

I was able to get to the competition’s page by myself. This page has cards for every team in the competition and they look like this

JavaScript

When clicked, those cards lead to that team’s page. This page contains a dropdown menu that looks like this

JavaScript

and this menu contains a div for each round of the competition. It looks like this

JavaScript

When clicked, each div loads that specific team’s formation, and its points during that round. The points are shown on the page like this:

JavaScript

My goal: I want to gather each team’s points during each one of the rounds in a dict[’round’] = points.

What I’ve tried already: I’ve tried to keep the teams in a list by doing

JavaScript

Then, for each team in teams I’d click on it. When on that page I’d find each round like this

JavaScript

Then, for each round in rounds I’d click on it and get that round’s points.

The problem: those loops where I iterate through teams and rounds are not working because apparently those webelements become stale after the whole process inside the loop (clicking, etc)

How can I approach this problem?

Advertisement

Answer

Angular drop down elements are rebuild in runtime. After drop down is collapsed – previously found drop down item is no longer an element of DOM. It is added to DOM one more time, when drop down is expanded again – but it is not the same element for WebDriver (even if it can be found with the same locator).

So, you’re following this logic:

  1. Expand drop down
  2. Get drop down elements ->
JavaScript
  1. Do something for each team -> here, I suppose, that drop down is collapsed. So found WebElements are no longer in DOM -> stale element exception

What you have to do?

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement