質問
下記コードを実行したときにStaleElementReferenceExceptionになります。
最初は出ていませんでしたがほぼ100%表示されるようになりました。
試した内容
sleepで待ち時間を設定 ダメでした
x_pathの要素を配列にいれてfor文に入れる ダメでした
for w in dd: ddd.append(w)
初心者なのでやり方がおかしいかもしれません…
対処方法教えていただけるとありがたいです。
下記がコードの一部とエラー内容です。
コード
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]").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 in ddd:
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])
エラー内容
---------------------------------------------------------------------------
StaleElementReferenceException Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_34836/1239864608.py in <module>
56 for date in ddd:
57 print(date)
---> 58 date_tmp_tmp = date.text
59 date_tmp = date_tmp_tmp.split(' ',1)
60 if re.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
79 def 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:
--> 425 self.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 def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=100.0.4896.127)
Stacktrace:
Backtrace:
Ordinal0 [0x004F9943+2595139]
Ordinal0 [0x0048C9F1+2148849]
Ordinal0 [0x00384528+1066280]
Ordinal0 [0x00386E04+1076740]
Ordinal0 [0x00386CBE+1076414]
Ordinal0 [0x00386F50+1077072]
Ordinal0 [0x003AC920+1231136]
Ordinal0 [0x003CB9EC+1358316]
Ordinal0 [0x003A7474+1209460]
Ordinal0 [0x003CBC04+1358852]
Ordinal0 [0x003DBAF2+1424114]
Ordinal0 [0x003CB806+1357830]
Ordinal0 [0x003A6086+1204358]
Ordinal0 [0x003A6F96+1208214]
GetHandleVerifier [0x0069B232+1658114]
GetHandleVerifier [0x0075312C+2411516]
GetHandleVerifier [0x0058F261+560433]
GetHandleVerifier [0x0058E366+556598]
Ordinal0 [0x0049286B+2173035]
Ordinal0 [0x004975F8+2192888]
Ordinal0 [0x004976E5+2193125]
Ordinal0 [0x004A11FC+2232828]
BaseThreadInitThunk [0x75F96739+25]
RtlGetFullPathName_UEx [0x772A8E7F+1215]
RtlGetFullPathName_UEx [0x772A8E4D+1165]