Please tell me how to deal with selenium's StaleElementReferenceExceptiont error

Asked 2 years ago, Updated 2 years ago, 370 views

Question

The StaleElementReferenceException occurs when you execute the code below.
It didn't come out at first, but it's almost 100% now.

Tried

  • Sleep failed to set the wait time

  • I couldn't put the elements of x_path in the array and put them in the for statement

    for window dd:
        ddd.append(w)
    

Failed to set wait time in sleep

Failed to put elements of x_path in array and for statement

for window dd:
    ddd.append(w)

I'm a beginner, so the way I do it may be strange...
I would appreciate it if you could tell me how to deal with it.

Below are some of the code and the error content.

Code

browser=webdriver.Chrome(executable_path=r"C:\Users\wakar\chromedriver.exe")

browser.get(url)

browser.find_element_by_xpath("/html/body/header/div/nav/ul/li[5]US>") .click()

browser.find_element_by_xpath("//*[@id='container']/main/div[2]/div/div/section/div[1]/form/input").send_keys(code)

browser.find_element_by_xpath("//*[@id='container']/main/div[2]/div/div/section/div[1]/form/button").click()

for i in range (click_times):
    browser.find_element_by_xpath("//*[@id='loading']/td/div/a").click()

ddd=browser.find_elements_by_xpath("//*[@id='container']/main/div[2]/div/div/section/div[2]/table/tbody/tr")

    
for date inddd:
    print(date)
    date_tmp_tmp = date.text
    date_tmp = date_tmp_tmp.split(',1)
    if re.compile('[0-9]{2}'+'/'[0-9]{2}').search(date_tmp[0]):
        date_h.append(date_tmp[0])

Error Contents

------------------------------------------------------------------------------------------------
StaleElementReferenceException Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_34836/1239864608.py in<module>
     56 for date inddd:
     57 print(date)
--- > 58 date_tmp_tmp = date.text
     59 date_tmp = date_tmp_tmp.split(',1)
     60 ifre.compile('[0-9]{2}'+'/'+'[0-9]{2}').search(date_tmp[0]):

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in text(self)
     75 def text(self)->str:
     76 "The text of the element."
--- >77 return self._execute (Command.GET_ELEMENT_TEXT) ['value']
     78 
     79def click(self)->None:

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in_execute(self, command, params)
    708 params = {}
    709 params ['id'] = self._id
-->710 return self._parent.execute (command, params)
    711 
    712 def find_element(self, by = By.ID, value = None):

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    423 response=self.command_executor.execute(driver_command, params)
    424 if response:
-->425self.error_handler.check_response(response)
    426 response ['value'] = self._unwrap_value(
    427 response.get('value', None))

~\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self,response)
    245 alert_text = value ['alert'].get('text')
    246 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore [call-arg] # mypy is not smart enough here
-->247 raise exception_class(message, screen, stacktrace)
    248 
    249 default_value_or_default(self, obj:Mapping[_KT,_VT], key:_KT, default:_VT)->_VT:

StaleElementReferenceException: Message:staleelement reference:element is not attached to the page document
  (Session info: chrome = 100.0.4896.127)
Stacktrace:
Backtrace:
    Original 0 [0x004F9943+2595139]
    Original 0 [0x0048C9F1+2148849]
    Original 0 [0x00384528+1066280]
    Original 0 [0x00386E04+1076740]
    Original 0 [0x00386CBE+1076414]
    Original 0 [0x00386F50+1077072]
    Original 0 [0x003AC920+1231136]
    Original 0 [0x003CB9EC+1358316]
    Original 0 [0x003A7474+1209460]
    Original 0 [0x003CBC04+1358852]
    Original 0 [0x003DBAF2+1424114]
    Original 0 [0x003CB806+1357830]
    Original 0 [0x003A6086+1204358]
    Original 0 [0x003A6F96+1208214]
    GetHandleVerifier [0x0069B232+1658114]
    GetHandleVerifier [0x0075312C+2411516]
    GetHandleVerifier [0x0058F261+560433]
    GetHandleVerifier [0x0058E366+556598]
    Original 0 [0x0049286B+2173035]
    Original 0 [0x004975F8+2192888]
    Original 0 [0x004976E5+2193125]
    Original 0 [0x004A11FC+2232828]
    BaseThreadInitThunk [0x75F96739+25]
    RtlGetFullPathName_UEx [0x772A8E7F+1215]
    RtlGetFullPathName_UEx [0x772A8E4D+1165]

python selenium

2022-09-30 22:03

1 Answers

How about ignoring exceptions in try?

from selenium.common.exception.exception import StaleElementReferenceException

for date inddd:
    try:
        print(date)
        date_tmp_tmp = date.text
        date_tmp = date_tmp_tmp.split(',1)
        if re.compile('[0-9]{2}'+'/'[0-9]{2}').search(date_tmp[0]):
            date_h.append(date_tmp[0])
    exceptStaleElementReferenceException:
        continue

Why don't you change the following parts? The line marked "#" is the changed line.

dd=browser.find_elements_by_xpath("//*[@id='container']/main/div[2]/div/div/section/div[2]/table/tbody/tr")

ddd_text = [date.text for date inddd]#

for date inddd_text:#
    print(date)
    date_tmp_tmp=date#
    date_tmp = date_tmp_tmp.split(',1)
    if re.compile('[0-9]{2}'+'/'[0-9]{2}').search(date_tmp[0]):
        date_h.append(date_tmp[0])


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.