尝试在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!'
任何帮助都将非常感激。
答案 0 :(得分:4)
试试这个:
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
答案 1 :(得分: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()