我使用Python Beautiful Soup来移动DOM元素和Selenium来打开chrome和ActionChains页面以在页面中滚动。它工作正常,但网站改变了一些东西,现在我遇到了两种错误,它在以前的工作方式和新的方式上作为一种可能的解决方案。
旧解决方案:
submit_button = driver.find_element_by_name('quantity')
elementList = submit_button.find_elements_by_tag_name("option")
elementList[int(column)-1].click()
旧错误:
Traceback (most recent call last):
File "C:/Users/Felix_P/PycharmProjects/Price_Tool/Combination_14_6_2016.py", line 205, in <module>
elementList[int(column)-1].click()
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 75, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 469, in _execute
return self._parent.execute(command, params)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
(Session info: chrome=51.0.2704.103)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64)
新解决方案:
submit_button = driver.find_element_by_name('quantity')
elementList = submit_button.find_elements_by_tag_name("option")
actions.move_to_element(elementList[int(column)-1]).click().perform()
新错误:
Traceback (most recent call last):
File "C:/Users/Felix_P/PycharmProjects/Price_Tool/Combination_14_6_2016.py", line 201, in <module>
actions.move_to_element(elementList[int(column)-1]).click().perform()
File "C:\Python35-32\lib\site-packages\selenium\webdriver\common\action_chains.py", line 72, in perform
action()
File "C:\Python35-32\lib\site-packages\selenium\webdriver\common\action_chains.py", line 217, in <lambda>
self._driver.execute(Command.MOVE_TO, {'element': to_element.id}))
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, 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=51.0.2704.103)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64)
答案 0 :(得分:0)
我想我看到了问题。如果你展示了所有的代码,它可能会显示最后一行是在一个导航离开页面的for循环内,然后返回到同一页面(或者某些ajax刷新了页面上的那组选项)。问题是页面已重新加载,因此elementList中的目标元素不再存在。
您需要通过跟踪选项上的值之类的列表来定义您的elementList,然后在循环中使用find_element再次查找每个唯一的选项元素。
答案 1 :(得分:0)
从您的错误堆栈中,它表明您保留的引用已过期。这通常是由于页面刷新或页面事件引起的。
因此,您必须在访问元素之前再次获取它。
例如,
elements = driver.find_elements_by_tag_name('tagname'))
for element in elements:
driver.get(element.get_attribute('url'))
将导致
selenium.common.exceptions.StaleElementReferenceException