【问题标题】:stale element reference: element is not attached to the page document for the drop down list - python过时的元素参考:元素未附加到下拉列表的页面文档 - python
【发布时间】:2017-09-24 04:12:08
【问题描述】:

我为一个 ASP.net 网站编写了 selenium chrome 驱动程序脚本,并在运行时遇到了stale element reference: element is not attached to the page document 错误。

脚本假设从下拉列表中逐项单击。总共有 4 个下拉列表,当页面加载时,第一个下拉列表有选项,当我们单击第一个下拉列表的第二个选项一段时间后,页面将数据加载到第二个下拉列表中,其他人的过程相同,所以通过下面的代码,我可以选择第二个,第三个,第四个......第一个下拉选项,然后在选择第二个下拉选项的第二个选项时出错

这里是代码

# -*- coding: utf-8 -*-

import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

def selectYear():
    year = driver.find_element_by_id("carYear")
    ActionChains(driver).move_to_element(year).click(year)
    year.click()
    WebDriverWait(year, 100).until(
        EC.presence_of_element_located((By.XPATH, ".//option[2]")))
    yearOptions = year.find_elements_by_tag_name("option")
    for option in yearOptions:
        optVal = option.get_attribute("value")
        if optVal is 0:
            continue
        else:
            print("Car Year is : %s" % optVal)
            dataList['Year'] = optVal
            ActionChains(driver).move_to_element(option).click(option)
            option.click()
            selectModel()

def selectMake(driver):
    make = driver.find_element_by_id("carMakeId")
    ActionChains(driver).move_to_element(make).click(make)
    make.click()
    WebDriverWait(make, 100).until(
        EC.presence_of_element_located((By.XPATH, ".//option[2]")))
    makeOptions = make.find_elements_by_tag_name("option")
    print makeOptions
    for option in makeOptions:
        optVal = option.get_attribute("value")
        if optVal == '0':
            continue
        else:
            print("Make is : %s" % optVal)
            dataList['Make'] = optVal
            ActionChains(driver).move_to_element(option).click(option)
            option.click()
            selectYear()


def main():
    driver.get("https://www.getunitronic.com/")
    WebDriverWait(driver, 200).until(
        EC.presence_of_element_located((By.ID, "carMakeId")))
    selectMake(driver)
# driver.close()

if __name__ == "__main__":
    dataList = {'Make' : "Make",
                'Year' : "Year",
                'Model' : "Model",
                'Engine' : "Engine",
                'Category' : "Category",
                'Product Type' : "Product Type",
                'Title' : "Title",
                'Stock Power' : "Stock Power",
                'Octane' : "Octane",
                'HP' : "HP",
                'LB-FT' : "LB-FT",
                'Desctiption' : "Description",
                'Installation' : "Installation",
                'UniCONNECT+' : "UniCONNECT+",
                'Features' : "Features",
                'Price' : "Price",
                'Media1' : "Media1",
                'Hardware Included' : "Hardware Included",
                'Recommended Software' : "Recommended Software",
                'Related Hardware' : "Related Hardware"}
    driver = webdriver.Chrome()
    sys.exit(main())

错误:

Make is : 8
Traceback (most recent call last):
  File ".\unitronic.py", line 121, in <module>
    sys.exit(main())
  File ".\unitronic.py", line 96, in main
    selectMake(driver)
  File ".\unitronic.py", line 85, in selectMake
    selectYear()
  File ".\unitronic.py", line 52, in selectYear
    EC.presence_of_element_located((By.XPATH, ".//option[2]")))
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
    value = method(self._driver)
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 63, in __call__
    return _find_element(driver, self.locator)    
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 402, in _find_element
    raise e
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.29.461591         (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.14393 x86_64)

【问题讨论】:

    标签: python selenium selenium-chromedriver


    【解决方案1】:

    更改您的代码

    makeOptions = make.find_elements_by_tag_name("option")
    print makeOptions
    for option in makeOptions:
        optVal = option.get_attribute("value")
        if optVal == '0':
            continue
        else:
            print("Make is : %s" % optVal)
            dataList['Make'] = optVal
            ActionChains(driver).move_to_element(option).click(option)
            option.click()
            selectYear()
    

    options_count = len(make.find_elements_by_tag_name("option"))
    
    print makeOptions
    for i in range(0, options_count):
        option = make.find_elements_by_tag_name("option")[i]
        optVal = option.get_attribute("value")
        if optVal == '0':
            continue
        else:
            print("Make is : %s" % optVal)
            dataList['Make'] = optVal
            ActionChains(driver).move_to_element(option).click(option)
            option.click()
            selectYear()
    

    元素集合中的元素一旦改变就会变得陈旧。所以遇到此类问题时需要重新获取元素

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 2017-12-13
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多