【问题标题】:Selenium clicks a link just once, calling click() another time returns errorSelenium 只点击一次链接,再次调用 click() 会返回错误
【发布时间】:2014-05-01 12:27:46
【问题描述】:

我是 Selenium 的新手,只是设法编写了这些代码。我想通过单击右下角的“>”链接来废弃表格上的数据。第一次单击有效,但接下来的两次无效。我错过了什么?谢谢。

# coding: utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Firefox()
browser.get('http://s.cafef.vn/Lich-su-giao-dich-HSG-1.chn')

next_page_link = browser.find_element_by_partial_link_text('>')
next_page_link.click()
next_page_link = browser.find_element_by_partial_link_text('>')
next_page_link.click()
next_page_link = browser.find_element_by_partial_link_text('>')
next_page_link.click()

这里是例外

Traceback (most recent call last):
  File "cafef.py", line 13, in <module>
    next_page_link.click()
  File "/usr/local/lib/python2.7/dist-packages/selenium-2.40.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 59, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python2.7/dist-packages/selenium-2.40.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 369, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium-2.40.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 164, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium-2.40.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: u'Element is no longer attached to the DOM' ; Stacktrace: 
    at fxdriver.cache.getElementAt (resource://fxdriver/modules/web_element_cache.js:7613)
    at Utils.getElementAt (file:///tmp/tmpIDOSdW/extensions/fxdriver@googlecode.com/components/command_processor.js:7210)
    at fxdriver.preconditions.visible (file:///tmp/tmpIDOSdW/extensions/fxdriver@googlecode.com/components/command_processor.js:8223)
    at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpIDOSdW/extensions/fxdriver@googlecode.com/components/command_processor.js:10861)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpIDOSdW/extensions/fxdriver@googlecode.com/components/command_processor.js:10878)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpIDOSdW/extensions/fxdriver@googlecode.com/components/command_processor.js:10883)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpIDOSdW/extensions/fxdriver@googlecode.com/components/command_processor.js:10825) 

【问题讨论】:

    标签: python selenium screen-scraping


    【解决方案1】:

    第二个next_page_link.click() 调用发生在浏览器加载下一页之前。 添加wait.untilEC.element_to_be_clickable

    from selenium import webdriver
    import selenium.webdriver.support.ui as UI
    from selenium.webdriver.common.by import By
    import selenium.webdriver.support.expected_conditions as EC 
    import contextlib
    
    with contextlib.closing(webdriver.Firefox()) as browser:
        browser.get('http://s.cafef.vn/Lich-su-giao-dich-HSG-1.chn')
        wait = UI.WebDriverWait(browser, 10)
        for i in range(3):            
            next_page_link = wait.until(
                EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, '>')))
            next_page_link.click()
    

    【讨论】:

      【解决方案2】:

      通常来自 unutbu 的 anwser 就足够了。

      我使用 selenium 和 .net 而不是 python,所以我不能给出 python 的详细代码,但为了稳定你应该等待 3 次:

      1. 等待下一页或 ajax 内容加载,在这种情况下您应该等到 .CafeF_Paging td span strong 的文本是您排除的页码。
      2. 等待 jquery 加载,直到 jQuery.active == 0
        查看http://sullerton.com/2013/08/selenium-webdriver-wait-for-ajax-with-python/
      3. 等待元素可见,不仅定位,使用 visibility_of_element_located。

      【讨论】:

        猜你喜欢
        • 2014-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多