How to set timeout for PhantomJS?

2019-05-11 08:52发布

问题:

Here's the code I have for setting it:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.resourceTimeout"] = ("5000")
driver = webdriver.PhantomJS(desired_capabilities=dcap)

However in my super long script, it doesn't seem to timeout when my internet is slow and a page takes longer than 5 seconds to load.

There is so little documentation on PhantomJS time outs, and even less of it is for Python, so I figure maybe this isn't even the way to do it.

Has anyone successfully set a timeout with PhantomJS in Python?

Thanks!

回答1:

instead of targeting phantomjs resource timeout, you can set a timeout for Driver like below example of Firefox driver:

browser = webdriver.Firefox()
browser.set_page_load_timeout(30)

you can change 30 to any numeric

Thanks



回答2:

You could use the default Python sleep method to retry a few times.

import time
time.sleep()


回答3:

FIREFOX = 'firefox'
PHANTOM = 'phantom'
NO_IMAGES = False
NEED_IMAGES = True
opened_pages_counter = 0
driver = None
details = {}
user_agent_mozilla = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
print("[x] UNIVERSAL_DRIVER module loaded")

if platform.system() == 'Windows':
    PHANTOMJS_PATH = 'd:/bin/phantomjs/bin/phantomjs.exe'
else:
    PHANTOMJS_PATH = './phantomjs'


@atexit.register
def cleanup():
    print("universal_driver:: on_exit")
    closeDriver()

#    PHANTOMJS_PATH = 'd:/bin/phantomjs/bin/phantomjs.exe'
#    driver_test = buildDriver(FIREFOX, NO_IMAGES )
# timeout need to be in seconds
#    driver_test = buildDriver(FIREFOX, NEED_IMAGES, timeout=100, width=100, height=50)

def buildDriver(driverType, needImages, **kwargs):
    global driver, details
    closeDriver()
    timeout = 60
    width=800
    height = 600
    x=0
    y=0
    for key in kwargs:
        print("another keyword arg: %s: %s" % (key, kwargs[key]))
        if key=="timeout":       timeout =  int(float(kwargs[key]))
        if key == "width": width = int(float(kwargs[key]))
        if key == "height": height = int(float(kwargs[key]))
        if key == "x": x = int(float(kwargs[key]))
        if key == "y": y = int(float(kwargs[key]))


    details['driverType'] = driverType
    details['needImages'] = needImages
    if driverType == FIREFOX:
        if driver == None:
            firefox_profile = webdriver.FirefoxProfile()
            if needImages == False:
                firefox_profile.set_preference('permissions.default.image', 2)
                firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')

            firefox_profile.set_preference("http.response.timeout", timeout)
            firefox_profile.set_preference("dom.max_script_run_time", timeout)
            driver = webdriver.Firefox(firefox_profile=firefox_profile)
            driver.set_window_size(width, height)
            return driver

    if driverType == PHANTOM:
        if driver == None:
            dcap = dict(DesiredCapabilities.PHANTOMJS)
            dcap["phantomjs.page.settings.userAgent"] = user_agent_mozilla
            dcap["phantomjs.page.settings.resourceTimeout"] = timeout*1000 # in mls
            # dcap["phantomjs.page.settings.loadImages"] = needImages
            # driver = webdriver.PhantomJS(desired_capabilities=dcap)
            service_argsA = []
            if needImages == False:
                service_argsA = ['--load-images=no']
            driver = webdriver.PhantomJS(PHANTOMJS_PATH, desired_capabilities=dcap, service_args=service_argsA)
            driver.set_window_size(width, height)
            return driver


def openPage(url):  # need to prevent of opening pages twice
    global driver
    global opened_pages_counter
    if driver == None:
        driver = buildDriver(PHANTOM, NO_IMAGES)
    if driver.current_url != url:
        driver.get(url)
        opened_pages_counter = opened_pages_counter + 1
        uprint("universal_driver::", details['driverType'], ", needImages:", details['needImages'], " ; page opened:", url)


def closeDriver():
    global driver
    if driver == None:
        return
    driver.close()
    driver.quit()
    driver = None
    uprint("universal_driver:: driver closed")


def uprint(*objects, sep=' ', end='\n', file=sys.stdout):
    enc = file.encoding
    if enc == 'UTF-8':
        print(*objects, sep=sep, end=end, file=file)
    else:
        f = lambda obj: str(obj).encode(enc, errors='backslashreplace').decode(enc)
        print(*map(f, objects), sep=sep, end=end, file=file)