Stale exception error while browsing through for loop

不羁岁月 提交于 2020-01-06 06:01:17

问题


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)

in the above code i get the stale exception error in the last for loop it runs for once but for the next value in the drop down i get the exception.I am trying to accomplish the task as the first drop-down describes the country, the second describes the states int he country and the third describes the cities in that state. I need to iterate i over all the cities in each state in each country but give me a stale exception error

as below:

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)

来源:https://stackoverflow.com/questions/51208052/stale-exception-error-while-browsing-through-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!