Phantomjs Selenium WebDriver中的自定义标头 [英] Custom headers in Phantomjs Selenium WebDriver

查看:78
本文介绍了Phantomjs Selenium WebDriver中的自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据,现在可以修改标头了. Atm我需要在PhantomJS webdriver中修改Accept-Language.此代码无效

According to this it is possible now to modify headers. Atm i need to modify Accept-Language in PhantomJS webdriver. This code doesn't work

DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Accept-Language'] = 'ru-RU'
driver = webdriver.PhantomJS()

是否可以通过某种方式配置Phantomjs以发送我的标头?我不在乎:在ghostdriver,phantomjs或phantomjs-webdriver中.

Is it possible somehow to configure Phantomjs to send my header? i don't care where: inside ghostdriver, phantomjs or phantomjs-webdriver.

推荐答案

最新版本( 1.9.1 )是2013年5月5日发行的.拉取请求已合并 2013年6月23日.

The latest version (1.9.1) of PhantomJS is release Jun/5/2013. The pull request is merged Jun/23/2013.

如果您使用的是PhantomJS 1.9.1版本,则自定义标头将不起作用.

If you are using 1.9.1 version of PhantomJS, custom headers will not work.

您必须自己构建phantomjs或等到phantomjs合并ghostdriver更改并发布新版本.

You have to build phantomjs yourself or wait until phantomjs merge ghostdriver changes and release new version.

  • 克隆PhantomJS存储库
  • 克隆ghostdriver存储库
  • 以递归方式将ghostdriver/src/*复制到phantomjs/src/ghostdriver
  • 构建phantomjs

使用新构建的phantomjs我得到以下结果:

Using newly build phantomjs I got following result:

from selenium import webdriver

webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Accept-Language'] = 'ru-RU'
driver = webdriver.PhantomJS()
driver.get('http://httpbin.org/headers')
print(driver.page_source)


...
{
  "headers": {
    "Connection": "close",
    "Host": "httpbin.org",
    "Accept-Encoding": "gzip",
    "Accept-Language": "ru-RU",
    "User-Agent": "Mozilla/5.0 (Unknown; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.10.0 (development) Safari/534.34",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  }
 ...

更新

使用PhantomJS 1.9.2 + .

这篇关于Phantomjs Selenium WebDriver中的自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆