Setting timeout on selenium webdriver.PhantomJS

后端 未结 2 604
醉酒成梦
醉酒成梦 2020-12-28 09:49

The situation

I have a simple python script to get the HTML source for a given url:

    browser = webdriver.PhantomJS()
            


        
相关标签:
2条回答
  • 2020-12-28 10:26

    Long Explanation below, so TLDR:

    Current version of Selenium's Ghostdriver (in PhantomJS 1.9.8) ignores resourceTimeout option, use webdriver's implicitly_wait(), set_page_load_timeout() and wrap them under try-except block.

    #Python
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    
    browser = webdriver.PhantomJS()
    browser.implicitly_wait(3)
    browser.set_page_load_timeout(3)
    try:
        browser.get("http://url_here")
    except TimeoutException as e:
        #Handle your exception here
        print(e)
    finally:
        browser.quit()
    

    Explanation

    To provide PhantomJS page settings to Selenium, one can use webdriver's DesiredCapabilities such as:

    #Python
    from selenium import webdriver
    cap = webdriver.DesiredCapabilities.PHANTOMJS
    cap["phantomjs.page.settings.resourceTimeout"] = 1000
    cap["phantomjs.page.settings.loadImages"] = False
    cap["phantomjs.page.settings.userAgent"] = "faking it"
    browser = webdriver.PhantomJS(desired_capabilities=cap)
    
    //Java
    DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
    capabilities.setCapability("phantomjs.page.settings.resourceTimeout", 1000);
    capabilities.setCapability("phantomjs.page.settings.loadImages", false);
    capabilities.setCapability("phantomjs.page.settings.userAgent", "faking it");
    WebDriver webdriver = new PhantomJSDriver(capabilities);
    

    But, here's the catch: As in today (2014/Dec/11) with PhantomJS 1.9.8 and its embedded Ghostdriver, resourceTimeout won't be applied by Ghostdriver (See the Ghostdriver issue#380 in Github).

    For a workaround, simply use Selenium's timeout functions/methods and wrap webdriver's get method in a try-except/try-catch block, e.g.

    #Python
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    
    browser = webdriver.PhantomJS()
    browser.implicitly_wait(3)
    browser.set_page_load_timeout(3)
    try:
        browser.get("http://url_here")
    except TimeoutException as e:
        #Handle your exception here
        print(e)
    finally:
        browser.quit()
    
    //Java
    WebDriver webdriver = new PhantomJSDriver();
    webdriver.manage().timeouts()
            .pageLoadTimeout(3, TimeUnit.SECONDS)
            .implicitlyWait(3, TimeUnit.SECONDS);
    try {
        webdriver.get("http://url_here");
    } catch (org.openqa.selenium.TimeoutException e) {
        //Handle your exception here
        System.out.println(e.getMessage());
    } finally {
        webdriver.quit();
    }
    
    0 讨论(0)
  • 2020-12-28 10:52

    PhantomJS has provided resourceTimeout, which might suit your needs. I quote from documentation here

    (in milli-secs) defines the timeout after which any resource requested will stop trying and proceed with other parts of the page. onResourceTimeout callback will be called on timeout.

    So in Ruby, you can do something like

    require 'selenium-webdriver'
    
    capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs("phantomjs.page.settings.resourceTimeout" => "5000")
    driver = Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities
    

    I believe in Python, it's something like (untested, only provides the logic, you are the Python developer, hopefully you will figure out)

    driver = webdriver.PhantomJS(desired_capabilities={'phantomjs.page.settings.resourceTimeout': '5000'})
    
    0 讨论(0)
提交回复
热议问题