3

尝试在 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!'

任何帮助将不胜感激。

4

2 回答 2

5

试试这个:

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
于 2015-11-22T05:27:03.997 回答
2
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()
于 2013-09-08T01:52:43.297 回答