在selenium webdriver.PhantomJ上设置超时

2024-05-20 16:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

情况

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

    browser = webdriver.PhantomJS()
    browser.get(url)
    content = browser.page_source

有时,url指向加载外部资源(如视频文件或广告内容)较慢的页面。

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

注意:出于无关的原因,我需要使用PhantomJS而不是requestsurllib2


问题

我想在PhantomJS资源加载时设置一个超时,以便如果资源加载时间太长,浏览器只假设它不存在或其他什么。

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

关于webdriver.PhantomJS的Documentation很薄,我还没有发现类似的问题。

提前谢谢!


Tags: browser脚本urlsource内容get源代码html
2条回答

PhantomJS提供了resourceTimeout,这可能适合您的需要。我引用了文档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.

所以在鲁比,你可以做一些

require 'selenium-webdriver'

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

我相信Python,它类似于(未经测试,只提供逻辑,您是Python开发人员,希望您能理解)

driver = webdriver.PhantomJS(desired_capabilities={'phantomjs.page.settings.resourceTimeout': '5000'})

下面是详细的说明,因此TLDR:

当前版本的Selenium的Ghostdriver(在PhantomJS 1.9.8中)忽略resourceTimeout选项,使用webdriver的隐式wait(),设置页面加载超时()并将它们包装在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()

说明

要向Selenium提供PhantomJSpage settings,可以使用webdriver所需的功能,例如:

#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);

但是,这里有一个问题:与今天(2014/12/11)PhantomJS 1.9.8及其嵌入的Ghostdriver一样,Ghostdriver(See the Ghostdriver issue#380 in Github)不会应用resourceTimeout。

对于解决方法,只需使用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()
//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();
}

相关问题 更多 >