【问题标题】:set a timout in python splinter web crawler在 python splinter 网络爬虫中设置超时
【发布时间】:2013-02-03 14:30:24
【问题描述】:

尝试在 python 中设置超时,就像在 ruby​​ 中一样。

我有一个链接,当我单击它时会打开一个弹出窗口,但我无法访问它,因为它会导致脚本冻结,直到我杀死它。几个月来,我一直在尝试访问这个弹出窗口,但对 ruby​​ watir-webdriver 并不满意。

我正在尝试超时调用弹出窗口,然后访问弹出窗口。

@timeout(3)
try:
b.execute_script("javascript:openMdlWindow('InvestmentDetailOptions.aspx?IDAssetType=','620','600');if(window.document.RetValue == '2'){window.parent.LoadinIframe('InvestmentDetail.aspx?FromMenu=N&IDAssetType=','Investment Details > Full View','false');}")
except Exception, e:
print 'timeout!'

任何帮助将不胜感激。

【问题讨论】:

  • 您将不得不提供更多上下文,例如您使用的框架。在这里使用装饰器也行不通。在将timeout 应用到它之前,您必须将代码包装在一个函数中。
  • 我正在使用 python 并将我要导航的站点拆分为 ASPX .Net。我能够启动弹出窗口,但之后无法访问弹出窗口,因为脚本只是挂起并且不会继续。我对 python 很陌生,正在尝试尽可能快地阅读。我不确定你在说什么功能。

标签: python selenium-chromedriver webautomation splinter


【解决方案1】:
import signal
from time import sleep

class TimeoutException(Exception):
    pass

def do_something_else():
    time = 5
    sleep(time)
    return 'do_something_else has to run for %d seconds' % time

def handler(signum, frame):
    raise TimeoutException 

def do_something_with_timeout(callback, timeout=3):
    signal.signal(signal.SIGALRM, handler)
    signal.alarm(timeout)
    try:
        value = callback()
        signal.alarm(0)
        return value
    except TimeoutException:
        pass
    signal.signal(signal.SIGALRM, signal.SIG_IGN)
    return 'time out'

def main():
    print 'hello'
    print do_something_with_timeout(do_something_else)
    print 'world'

main()

【讨论】:

    【解决方案2】:

    试试这个:

    from splinter import Browser
    from selenium.common.exceptions import TimeoutException
    b = Browser('firefox')
    b.driver.set_page_load_timeout(1)
    try:
       b.visit('http://www.bbc.com')
    except TimeoutException:
       pass
    print b.html
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      相关资源
      最近更新 更多