浏览for循环时出现陈旧异常错误

时间:2018-07-06 10:11:04

标签: python-3.x selenium-webdriver web-scraping staleelementreferenceexception

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
path_to_chromedriver = 'C:/Users/WIN7/AppData/Local/Programs/Python/Python37-32/chromedriver.exe'
browser = webdriver.Chrome(executable_path=path_to_chromedriver)
browser.get('https://shipped.com/shipping-containers-for-sale.php')
Country_Click = browser.find_element_by_xpath("//select[@name='country']")
all_options = Country_Click.find_elements_by_tag_name("option")
for option in all_options:
    print("Value is: %s" % option.get_attribute("value"))
    option.click()
    browser.implicitly_wait(20)
    state_click = browser.find_element_by_xpath("//select[@name='state']")
    all_options1 = state_click.find_elements_by_tag_name("option")
    for option in all_options1:
        print("Value is: %s" % option.get_attribute("value"))
        option.click()
        city_click = browser.find_element_by_xpath("//select[@name='city']")
        all_options2 = city_click.find_elements_by_tag_name("option")
        browser.implicitly_wait(20)
        for option in all_options2:
            print("Value is: %s" % option.get_attribute("value"))
            option.click()
            time.sleep(20)
            browser.implicitly_wait(20)
            rows = browser.find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')
        states = []
        for row in rows:
            cells = row.find_elements_by_tag_name('td')
            SizeOfContainer = cells[0].text.replace('Double \n Doors', "")
            SizeOfContainer1 = SizeOfContainer.replace('HC', "")
            SizeOfContainer2 = SizeOfContainer1.replace('Double Doors', "")
            SizeOfContainer3 = SizeOfContainer2.replace('\n', "")
            SizeOfContainer4 = SizeOfContainer3.replace('DoubleDoors', "")
            Ranking = cells[1].text
            Price = cells[2].text
            DeliveryAvailable = cells[3].text

            Final = (SizeOfContainer4.replace("What's this?","") + "," + Ranking + "," + Price.replace(",", "|") + "," + DeliveryAvailable.replace(",", "|") + "\n")
            Output = Final.replace(
                #"United States,Alabama,Want More?,  10' \n to \n 53', Rare containers + common sizes. High-\ncube or standard, we've got you covered.,  Request A Quote »","")

            print(Output)

在上面的代码中,我在上一个for循环中获得了过时的异常错误,它只运行了一次,但是对于下拉列表中的下一个值,我得到了异常。我正在尝试完成任务,如第一个下拉列表所述国家,第二个描述该国家的州,第三个描述该州的城市。 我需要遍历每个国家每个州的所有城市,但给我一个过时的异常错误

如下:

Traceback (most recent call last):
  File "C:/Users/WIN7/.PyCharmCE2018.1/config/scratches/FinalShipped.py", line 24, in <module>
    print("Value is: %s" % option.get_attribute("value"))
  File "C:\Users\WIN7\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 143, in get_attribute
    resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})
  File "C:\Users\WIN7\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\WIN7\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\WIN7\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, 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=67.0.3396.99)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 6.1.7601 SP1 x86_64)

0 个答案:

没有答案