0

I can watch execute_script() run many many times in the AngularJS routed single page I'm testing since it seems a lot of the code uses execute_script() to talk to the browser.

The specific call is:

driver.execute_script("return window.dataLayer")

Which works many times successfully.

But at some point, that same command receives a 404 from Selenium. I dug into the python selenium:

Deep in urllib3 it's POSTing this same as every other time:

ipdb> urlopen_kw                                                                                                
{'body': '{"script": "return window.dataLayer", "args": []}', 'request_url': 'http://127.0.0.1:51673/session/<sessionidxxx>/execute/sync'} 

But at the same place in the test, suddenly the response from the request is:

ipdb> data
'{"value":{"error":"stale element reference","message":"stale element reference: stale element not found\\n  (Session info: chrome=84.0.4147.105)","stacktrace":"#0 0x561877802d99 \\u003C
unknown>\\n"}}'

I'm on Ubuntu:

/usr/bin❯ chromium-browser --version
Using PPAPI flash.
Chromium 84.0.4147.105 Built on Ubuntu , running on Ubuntu 18.04

/usr/bin❯ chromedriver --version    
ChromeDriver 84.0.4147.30

That ChromeDriver was found via: https://chromedriver.chromium.org/downloads/version-selection

Using pdb, I'm still perfectly able to use execute_script(<command>) to interact with the page and everything on it. And the URL isn't changing and the page is not refreshing.

It just seems that somehow the specific window.dataLayer (while still present and complete in the browser dev console) is "stale". Is it cached somewhere? In the webdriver? Is there a way to stop that?

The only connection I can make is that when I only used the dataLayer object for one evaluation it seems to NOT break. But I enhanced the code to look at the dataLayer in a loop (after acquiring it once). It would not immediately go stale. But after a larger interaction triggering XHR and other requests, suddenly it says it's stale. Very confusing.

I hope this is enough information for somebody to recognize what might be the cause.

Thanks much!

iJames
  • 640
  • 1
  • 7
  • 16

1 Answers1

0

As you are looping through elements while performing some actions on them. Its look like your page is getting refreshed. As a result either elements you have stored earlier are either getting reloaded or lost altogether. One of technique of overcoming stale element is loop is, for every iteration use a fresh locator for your element and access it with the help of iteration number of your loop.

rahul rai
  • 2,260
  • 1
  • 7
  • 17
  • Share the HTML DOM of your elements, so that i can try in my local. – rahul rai Aug 22 '20 at 07:01
  • Thanks! In this case it's not a DOM element, it's a property on the window object itself. If I pause execution. The `window.dataLayer` is still present and populated as I can still inspect it from the browser directly. Also, when it fails, it happens immediately, not during the looping. But you are making me think somehow I need to isolate this by reducing the steps of the test and the complexity of the page until I can get it to work! – iJames Aug 22 '20 at 23:29