selenium(python)引发StaleElementReferenceException并且不会继续下载所有webdriver.find_elements_by_partial_link_text()

时间:2015-01-11 16:25:10

标签: python selenium web web-scraping

我正在使用pylen的selenium绑定来下载页面上包含字符串“VS”的所有链接。问题是列表中的第二项不是有效的网页(返回404错误),如果我手动点击它返回的断开的链接:

error.html - 404 error page does not exist.

如果我运行以下代码,则会引发错误。

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException, NoSuchWindowException

# To prevent download dialog
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)  # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/msword, application/vnd.ms-powerpoint')

driver = webdriver.Firefox(profile)
driver.get("http://www.SOME_URL.com/")

links = driver.find_elements_by_partial_link_text("VS")

for link in links:
    url = link.get_attribute("href")
    try:
        driver.get(url)
    except StaleElementReferenceException:
       pass

错误:

Traceback (most recent call last):
  File "C:\Users\lskrinjar\Dropbox\work\preracun\src\web_data_mining\get_files_from_web.py", line 79, in <module>
    url = link.get_attribute("href")
  File "C:\Python27\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 93, in get_attribute
    resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})
  File "C:\Python27\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 385, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
    at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:8329:1)
    at Utils.getElementAt (file:///c:/users/lskrin~1/appdata/local/temp/tmppsn7tm/extensions/fxdriver@googlecode.com/components/command-processor.js:7922:10)
    at WebElement.getElementAttribute (file:///c:/users/lskrin~1/appdata/local/temp/tmppsn7tm/extensions/fxdriver@googlecode.com/components/command-processor.js:11107:31)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/lskrin~1/appdata/local/temp/tmppsn7tm/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:16)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/lskrin~1/appdata/local/temp/tmppsn7tm/extensions/fxdriver@googlecode.com/components/command-processor.js:548:5)

0 个答案:

没有答案