使用Python Selenium收到“ selenium.common.exceptions.StaleElementReferenceException”错误

时间:2019-05-18 15:16:48

标签: python selenium

我试图获取一个下拉字段的所有值,然后遍历这些值。循环时,我试图选择每个下拉值。现在,对于每个选择,将填充另一个下拉字段。因此,这是一个相关的下拉字段。

到目前为止,我可以获取所有下拉列表值,并且可以遍历这些值。但是面临的主要问题是当我选择每个值时。当我选择每个值时,它将发送一个发布请求,刷新整个页面,并使用值填充相关的下拉菜单。

现在,我在python脚本下使用此

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#from selenium.webdriver.support.expected_conditions import stalenessOf

import time

driver = webdriver.Firefox()
driver.set_script_timeout(10)

wait = WebDriverWait(driver, 10)

driver.get("https://test.aspx")

district_list = Select(driver.find_element_by_name("DistrictList"))

for district in district_list.options:
    wait.until(EC.visibility_of_element_located((By.NAME, 'DistrictList')))
    district_name = district.get_attribute("text")
    print("District is: %s" % district_name)
    district_list.select_by_visible_text(district_name)
    wait.until(EC.visibility_of_element_located((By.NAME, 'DistrictList')))

但我收到此错误

selenium.common.exceptions.StaleElementReferenceException:消息:的元素引用是陈旧的;要么元素不再附加到DOM,它不在当前框架上下文中,要么文档已刷新

编辑: 完整的错误响应:

(base) F:\Projects\GitHub\Capture_Ocr>python selenium_dropdown.py
District is: -- Select District --
District is: Ahmednagar
Traceback (most recent call last):
  File "selenium_dropdown.py", line 20, in <module>
    district_name = district.get_attribute("text")
  File "C:\Users\Kapil\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 141, in get_attribute
    self, name)
  File "C:\Users\Kapil\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script
    'args': converted_args})['value']
  File "C:\Users\Kapil\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Kapil\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of <option> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed

现在,我在StackOverflow上浏览了一些链接,例如

Python Selenium: wait until an element is no longer stale?

和其他人,但找不到任何解决方案。

2 个答案:

答案 0 :(得分:1)

@Cory Goldberg解释了过时元素异常的根本原因。这是解决此问题的解决方案。

# get number of options in the district list
distrOptions= len(driver.find_elements_by_xpath("//select[@name='DistrictList']/option")))

# loop through all the options using the option index
for distNum in range(distrOptions):
    wait.until(EC.visibility_of_element_located((By.NAME, 'DistrictList')))
    # get the district element based on index
    districtEle = driver.find_element_by_xpath("(//select[@name='DistrictList']/option)[" + str(distNum+1) +"]"))
    # get district name
    district_name = districtEle.get_attribute("text")
    # print the district Name
    print("District is: %s" % district_name)
    # select district from the list box
    districtEle.click()
    wait.until(EC.visibility_of_element_located((By.NAME, 'DistrictList')))

答案 1 :(得分:0)

  

它发送一个发布请求,刷新   整个页面并填充依赖项   带有值的下拉菜单。

说明:

在加载新页面时,您将丢失对前一页上所有元素的引用...因此,在您的循环中第二次尝试访问原始页面上的元素,它不再存在...所以您得到一个StaleElementReferenceException

基本上,在遍历元素时您将无法执行任何触发导航的操作,否则您将获得此操作。每次页面重新加载时,您都需要获取新元素。