如何修复 Python Selenium 上的 StaleElementReferenceException?
How to fix StaleElementReferenceException on Python Selenium?
我在 python 中使用 Selenium 时遇到以下错误:
Traceback (most recent call last):
File "C:\Users\pedro\Desktop\PyCharm\tutorial\index.py", line 30, in <module>
select_object_unidades.select_by_value(unidades)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\support\select.py", line 79, in select_by_value
opts = self._el.find_elements(By.CSS_SELECTOR, css)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 761, in find_elements
return self._execute(Command.FIND_CHILD_ELEMENTS,
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 710, in _execute
return self._parent.execute(command, params)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
self.error_handler.check_response(response)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, 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: chrome=101.0.4951.41)
Stacktrace:
Backtrace:
Ordinal0 [0x0107B8F3+2406643]
Ordinal0 [0x0100AF31+1945393]
Ordinal0 [0x00EFC748+837448]
Ordinal0 [0x00EFF154+848212]
Ordinal0 [0x00EFF012+847890]
Ordinal0 [0x00EFF2A0+848544]
Ordinal0 [0x00F28FF5+1019893]
Ordinal0 [0x00F2957B+1021307]
Ordinal0 [0x00F1FF41+982849]
Ordinal0 [0x00F442C4+1131204]
Ordinal0 [0x00F1FA64+981604]
Ordinal0 [0x00F44494+1131668]
Ordinal0 [0x00F54682+1197698]
Ordinal0 [0x00F44096+1130646]
Ordinal0 [0x00F1E636+976438]
Ordinal0 [0x00F1F546+980294]
GetHandleVerifier [0x012E9612+2498066]
GetHandleVerifier [0x012DC920+2445600]
GetHandleVerifier [0x01114F2A+579370]
GetHandleVerifier [0x01113D36+574774]
Ordinal0 [0x01011C0B+1973259]
Ordinal0 [0x01016688+1992328]
Ordinal0 [0x01016775+1992565]
Ordinal0 [0x0101F8D1+2029777]
BaseThreadInitThunk [0x76BEFA29+25]
RtlGetAppContainerNamedObjectPath [0x77C47A7E+286]
RtlGetAppContainerNamedObjectPath [0x77C47A4E+238]
相关部分是:
select_element_nivel_ensino = driver.find_element(By.ID, "formTurma:inputNivel")
select_object_nivel_ensino = Select(select_element_nivel_ensino)
all_available_options_nivel_ensino = create_list_nivel_ensino()
select_element_unidades = driver.find_element(By.ID, "formTurma:inputDepto")
select_object_unidades = Select(select_element_unidades)
all_available_options_unidades = create_list_unidades()
for nivel_ensino in all_available_options_nivel_ensino:
select_object_nivel_ensino.select_by_value(nivel_ensino)
for unidades in all_available_options_unidades:
select_object_unidades.select_by_value(unidades)
driver.find_element(By.NAME, "formTurma:j_id_jsp_1370969402_11").click()
错误发生在第二个 FOR 的末尾,点击后,运行 只循环一次,并给出错误在那里显示。
这个错误是什么意思,我该如何解决?
解决方案:
all_available_options_nivel_ensino = create_list_nivel_ensino()
all_available_options_unidades = create_list_unidades()
for nivel_ensino in all_available_options_nivel_ensino:
select_element_nivel_ensino = driver.find_element(By.ID, "formTurma:inputNivel")
select_object_nivel_ensino = Select(select_element_nivel_ensino)
select_object_nivel_ensino.select_by_value(nivel_ensino)
for unidades in all_available_options_unidades:
select_element_unidades = driver.find_element(By.ID, "formTurma:inputDepto")
select_object_unidades = Select(select_element_unidades)
select_object_unidades.select_by_value(unidades)
driver.find_element(By.NAME, "formTurma:j_id_jsp_1370969402_11").click()
我在 python 中使用 Selenium 时遇到以下错误:
Traceback (most recent call last):
File "C:\Users\pedro\Desktop\PyCharm\tutorial\index.py", line 30, in <module>
select_object_unidades.select_by_value(unidades)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\support\select.py", line 79, in select_by_value
opts = self._el.find_elements(By.CSS_SELECTOR, css)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 761, in find_elements
return self._execute(Command.FIND_CHILD_ELEMENTS,
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 710, in _execute
return self._parent.execute(command, params)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
self.error_handler.check_response(response)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, 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: chrome=101.0.4951.41)
Stacktrace:
Backtrace:
Ordinal0 [0x0107B8F3+2406643]
Ordinal0 [0x0100AF31+1945393]
Ordinal0 [0x00EFC748+837448]
Ordinal0 [0x00EFF154+848212]
Ordinal0 [0x00EFF012+847890]
Ordinal0 [0x00EFF2A0+848544]
Ordinal0 [0x00F28FF5+1019893]
Ordinal0 [0x00F2957B+1021307]
Ordinal0 [0x00F1FF41+982849]
Ordinal0 [0x00F442C4+1131204]
Ordinal0 [0x00F1FA64+981604]
Ordinal0 [0x00F44494+1131668]
Ordinal0 [0x00F54682+1197698]
Ordinal0 [0x00F44096+1130646]
Ordinal0 [0x00F1E636+976438]
Ordinal0 [0x00F1F546+980294]
GetHandleVerifier [0x012E9612+2498066]
GetHandleVerifier [0x012DC920+2445600]
GetHandleVerifier [0x01114F2A+579370]
GetHandleVerifier [0x01113D36+574774]
Ordinal0 [0x01011C0B+1973259]
Ordinal0 [0x01016688+1992328]
Ordinal0 [0x01016775+1992565]
Ordinal0 [0x0101F8D1+2029777]
BaseThreadInitThunk [0x76BEFA29+25]
RtlGetAppContainerNamedObjectPath [0x77C47A7E+286]
RtlGetAppContainerNamedObjectPath [0x77C47A4E+238]
相关部分是:
select_element_nivel_ensino = driver.find_element(By.ID, "formTurma:inputNivel")
select_object_nivel_ensino = Select(select_element_nivel_ensino)
all_available_options_nivel_ensino = create_list_nivel_ensino()
select_element_unidades = driver.find_element(By.ID, "formTurma:inputDepto")
select_object_unidades = Select(select_element_unidades)
all_available_options_unidades = create_list_unidades()
for nivel_ensino in all_available_options_nivel_ensino:
select_object_nivel_ensino.select_by_value(nivel_ensino)
for unidades in all_available_options_unidades:
select_object_unidades.select_by_value(unidades)
driver.find_element(By.NAME, "formTurma:j_id_jsp_1370969402_11").click()
错误发生在第二个 FOR 的末尾,点击后,运行 只循环一次,并给出错误在那里显示。
这个错误是什么意思,我该如何解决?
解决方案:
all_available_options_nivel_ensino = create_list_nivel_ensino()
all_available_options_unidades = create_list_unidades()
for nivel_ensino in all_available_options_nivel_ensino:
select_element_nivel_ensino = driver.find_element(By.ID, "formTurma:inputNivel")
select_object_nivel_ensino = Select(select_element_nivel_ensino)
select_object_nivel_ensino.select_by_value(nivel_ensino)
for unidades in all_available_options_unidades:
select_element_unidades = driver.find_element(By.ID, "formTurma:inputDepto")
select_object_unidades = Select(select_element_unidades)
select_object_unidades.select_by_value(unidades)
driver.find_element(By.NAME, "formTurma:j_id_jsp_1370969402_11").click()