这是我设置它的代码:

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)

但是在我的超长脚本中,当我的互联网很慢并且页面加载时间超过5秒时,它似乎没有超时。

关于PhantomJS超时的文档很少,对Python来说更少,所以我想也许这甚至不是这样做的。

有没有人用Python在PhantomJS中成功设置超时?

谢谢!

===============>>#1 票数:5 已采纳

而不是针对phantomjs资源超时,您可以设置驱动程序的超时,如下面的Firefox驱动程序示例:

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

你可以改变30到任何数字

谢谢

===============>>#2 票数:0

您可以使用默认的Python sleep方法重试几次。

import time
time.sleep()

===============>>#3 票数:0

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)

  ask by User translate from so

未解决问题?本站智能推荐: