set a timout in python splinter web crawler

2019-04-12 04:58发布

Trying to set a time out in python much like you would in ruby.

I have a link that when I click it opens a popup but I can not access it because it causes the script to freeze until I kill it. I have been trying for months to access this popup to no joy in ruby watir-webdriver.

I am trying to timeout the call to popup and then access the popup window.

@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!'

any help will be dearly appreciated.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-12 05:20

Just try this:

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
查看更多
smile是对你的礼貌
3楼-- · 2019-04-12 05:44
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()
查看更多
登录 后发表回答