Setting timeout on selenium webdriver.PhantomJS

后端 未结 2 605
醉酒成梦
醉酒成梦 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条回答
  •  -上瘾入骨i
    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();
    }
    

提交回复
热议问题