2017-09-24 38 views
0

我已經寫了一個ASP.net網站的硒鉻驅動程序腳本,並得到了stale element reference: element is not attached to the page document錯誤,同時運行它。陳舊元素引用:元素沒有附加到頁面文檔的下拉列表 - python

腳本假設從下拉列表中單擊一個項目。總共有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) 

回答

0

更改您的c一旦被改變頌歌從

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() 

元素將變得陳舊。所以當你遇到這樣的問題時你需要再次獲取元素

相關問題