Selenium,python,定期点击javascript链接? [英] Selenium, python, clicking on a javascript link regularly?

查看:91
本文介绍了Selenium,python,定期点击javascript链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python和selenium绑定在常规时间间隔点击javascript生成的链接应该是什么样的正确方法?它应该使用线程吗?

what should be right way to click on a javascript generated link on a regular time interval using python and selenium bindings? should it be using a thread?

因为我需要继续处理输入数据,我需要刷新/重置计时器以继续接收数据,点击这个给定的链接进行刷新(此链接是由javascript直接生成的html)。

as i would need to continue to process the input data, i need to refresh/reset a timer to continue to receive data, clicking on this given link to do this refresh (and this link is html directly generated by javascript).

最好的问候

推荐答案

你不需要线程来做这件事。

You don't need thread to do this.

使用javascript函数 setInterval 以持续点击该链接。

Use javascript function setInterval to continuously click the link.

例如:

import time

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://jsfiddle.net/falsetru/4UxgK/show/')

# Click the link every 3000 ms.
driver.execute_script('''
    // argument passed from Python can be accessed by `arguments` array.
    var link = arguments[0];
    var timer = setInterval(function() {
        link.click();
    }, 3000);
''', driver.find_element_by_id('activity'))

while True:
    data = driver.find_element_by_id('counter').text
    print(data)
    time.sleep(1)

注意

如果收到如下错误,请升级 selenium 到最新版本。我在 Firefox 23.0 + selenium 2.32.0 中遇到以下错误。错误消失了 selenium 2.35.0

If you get error like follow, upgrade selenium to recent version. I experienced following error with Firefox 23.0 + selenium 2.32.0. Error was gone with selenium 2.35.0.

Traceback (most recent call last):
  File "t2.py", line 12, in <module>
    print driver.execute_script('''return 1 + 2;''')
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 397, in execute_script
    {'script': script, 'args':converted_args})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: u'waiting for evaluate.js load failed' ; Stacktrace: 
    at r (file:///tmp/tmpm1sJhH/extensions/fxdriver@googlecode.com/components/driver_component.js:8360)
    at fxdriver.Timer.prototype.runWhenTrue/g (file:///tmp/tmpm1sJhH/extensions/fxdriver@googlecode.com/components/driver_component.js:392)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpm1sJhH/extensions/fxdriver@googlecode.com/components/driver_component.js:386) 



替代方案:使用线程



Alternative: using thread

import threading
import time

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://jsfiddle.net/falsetru/4UxgK/show/')

def click_loop(link, interval):
    while True:
        link.click()
        time.sleep(interval)

link = driver.find_element_by_id('activity')
threading.Thread(target=click_loop, args=(link, 3)).start()

while True:
    data = driver.find_element_by_id('counter').text
    print(data)
    time.sleep(1)

这篇关于Selenium,python,定期点击javascript链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆