在selenium webdriver.PhantomJS上设置超时

toh*_*ter 9 python selenium phantomjs selenium-webdriver

情况

我有一个简单的python脚本来获取给定URL的HTML源:

    browser = webdriver.PhantomJS()
    browser.get(url)
    content = browser.page_source
Run Code Online (Sandbox Code Playgroud)

有时,该网址指向具有缓慢加载的外部资源的页面(例如视频文件或非常慢的广告内容).

在完成.get(url)请求之前,Webdriver将一直等到加载这些资源.

注意:由于无关紧要的原因,我需要使用PhantomJS而不是requests或者urllib2


这个问题

我想在PhantomJS资源加载上设置超时,这样如果资源加载时间过长,浏览器就会认为它不存在或者其他什么.

这将允许我.pagesource根据浏览器加载的内容执行后续查询.

webdriver.PhantomJS上的文档很薄,我没有在SO上找到类似的问题.

提前致谢!

Ewy*_*ato 17

下面的长解释,所以TLDR:

当前版本的Selenium的Ghostdriver(在PhantomJS 1.9.8中)忽略resourceTimeout选项,使用webdriver的implicitly_wait(),set_page_load_timeout()并将它们包装在try-except块下.

#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()
Run Code Online (Sandbox Code Playgroud)

说明

要向Selenium 提供PhantomJS 页面设置,可以使用webdriver的DesiredCapabilities,例如:

#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)
Run Code Online (Sandbox Code Playgroud)
//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);
Run Code Online (Sandbox Code Playgroud)

但是,这里有一个问题:就像今天(2014/Dec/11)中的PhantomJS 1.9.8及其嵌入式Ghostdriver一样,Ghostdriver不会应用resourceTimeout(参见Github中的Ghostdriver问题#380).

要解决此问题,只需使用Selenium的超时函数/方法,并在try-except/try-catch块中包装webdriver的get方法,例如

#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()
Run Code Online (Sandbox Code Playgroud)
//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();
}
Run Code Online (Sandbox Code Playgroud)


Yi *_*eng 11

PhantomJS已提供resourceTimeout,可能适合您的需求.我在这里引用文档

(以毫秒为单位)定义超时,在此超时之后,任何请求的资源将停止尝试并继续执行页面的其他部分.onResourceTimeout回调将在超时时调用.

所以在Ruby中,你可以做类似的事情

require 'selenium-webdriver'

capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs("phantomjs.page.settings.resourceTimeout" => "5000")
driver = Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities
Run Code Online (Sandbox Code Playgroud)

我相信Python,它就像(未经测试,只提供逻辑,你是Python开发人员,希望你会弄清楚)

driver = webdriver.PhantomJS(desired_capabilities={'phantomjs.page.settings.resourceTimeout': '5000'})
Run Code Online (Sandbox Code Playgroud)