Selenium, Python, exceptions.StaleElementReferenceException Error message

Viewed 56

I have been following a video on YT how to create a PS5 Stock bot that checks if the product is back in stock, but the last part on sending a notification was not uploaded (Part 3) but thanks to the lessons that I had on team treehouse is enabled to solve that my self until this error showed up and was hoping some could help me.

How to script runs

The script runs on https://wayscript.com/ and runs as follow:

Time Trigger > HTTP Trigger > HTTP Response > Python script > IF > Send message to discord server.

Here are the video's I am referring to

Part 1: https://www.youtube.com/watch?v=4rHqekQeIn0&t=248s

Part 2: https://www.youtube.com/watch?v=d3YTRvAoa3U

Part 3: Not Published

About the Error

The error happens not in the same order most of the time the script runs 2/3 times before the error shows up and the error does also not stay most of the time the error repeats himself 2x and after that, the script run fine for 2 or 3 times and it repeats itself.

where do I need help with:

I need help with solving the error.

Python script:

from selenium import webdriver
import csv 
from datetime import date



options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0')



browser = webdriver.Chrome(options = options)
browser.set_page_load_timeout(60)


browser.get('https://www.wehkamp.nl/sony-playstation-5-16644186/')

all_span = browser.find_elements_by_tag_name("span")
all_span = [i.text for i in all_span]

date = date.today()
is_beschikbaar = ""

if "uitverkocht" in all_span:
    print("Playstation 5 is niet op voorraad")
    is_beschikbaar = False
else: 
    print("Playstation 5 is weer op voorraad")
    is_beschikbaar = True
    
    
headers = ["date", "is de Playstation 5 Beschikbaar?"]

with open ("ps5_beschikbaarheid.csv", "a" ) as csvfile:
    write = csv.writer(csvfile)
    #write.writerow(headers)
    write.writerow([date, is_beschikbaar])
    
variables['text'] = is_beschikbaar 
    
browser.close()

Error message

12:19:00    Finished running
12:19:56    Time Trigger ran.
12:19:56    Executing your code
12:19:56    Playstation 5 is niet op voorraad
12:19:56    Executed your code
12:19:56    If statement is false.
12:19:56    Sleep ran.
12:19:56    Finished running


12:21:00    Time Trigger ran.
12:21:00    Executing your code

            Traceback (most recent call last):
            File "python_step", line 22, in <module>
                all_span = [i.text for i in all_span]
            File "python_step", line 22, in <listcomp>
                all_span = [i.text for i in all_span]
            File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 76, in text
                return self._execute(Command.GET_ELEMENT_TEXT)['value']
            File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
12:21:00          return self._parent.execute(command, params)
            File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
                self.error_handler.check_response(response)
            File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
                raise exception_class(message, screen, stacktrace)
            selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
            (Session info: headless chrome=92.0.4515.159)

12:21:00    SCRIPT TERMINATED: Script haulted due to an error from the python_code module.
12:21:00    Finished running
1 Answers

i suggest you to rewrite the both lines:

all_span = browser.find_elements_by_tag_name("span")
all_span = [i.text for i in all_span]

by:

all_spans = browser.find_elements_by_tag_name("span")
nbrspans = len(all_spans)
all_texts = []
for i in range(nbrspans):
    all_spans = browser.find_elements_by_tag_name("span")
    all_texts.append(all_spans[i].text)

and after you deal with all_texts

the error means you have to reload the the search of spans

Related