简体   繁体   English

Phantomjs Selenium WebDriver中的自定义标题

[英]Custom headers in Phantomjs Selenium WebDriver

According to this it is possible now to modify headers. 根据这个 ,现在可以修改标题。 Atm i need to modify Accept-Language in PhantomJS webdriver. 我需要在PhantomJS webdriver中修改Accept-Language。 This code doesn't work 此代码不起作用

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

Is it possible somehow to configure Phantomjs to send my header? 有可能以某种方式配置Phantomjs发送我的标题? i don't care where: inside ghostdriver, phantomjs or phantomjs-webdriver. 我不关心在哪里:内部ghostdriver,phantomjs或phantomjs-webdriver。

The latest version ( 1.9.1 ) of PhantomJS is release Jun/5/2013. PhantomJS的最新版本( 1.9.1 )将于2013年6月5日发布。 The pull request is merged Jun/23/2013 . 拉取请求于2013年6月23日合并。

If you are using 1.9.1 version of PhantomJS, custom headers will not work. 如果您使用的是1.9.1版本的PhantomJS,则自定义标头将不起作用。

You have to build phantomjs yourself or wait until phantomjs merge ghostdriver changes and release new version. 你必须自己构建phantomjs或等到phantomjs合并ghostdriver更改并发布新版本。

  • Clone PhantomJS repository 克隆PhantomJS存储库
  • Clone ghostdriver repository 克隆ghostdriver存储库
  • copy ghostdriver/src/* to phantomjs/src/ghostdriver recursively 将ghostdriver / src / *复制到phantomjs / src / ghostdriver递归
  • build phantomjs 建立phantomjs

Using newly build phantomjs I got following result: 使用新建的phantomjs我得到以下结果:

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"
  }
 ...

UPDATE UPDATE

Use PhantomJS 1.9.2+ . 使用PhantomJS 1.9.2+

I write a full example to set all headers , window size and proxy in selenium phantomjs: 我写了一个完整的例子来设置selenium phantomjs中的所有头文件,窗口大小和代理:

from selenium import webdriver

def init_phantomjs_driver(*args, **kwargs):

    headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0',
        'Connection': 'keep-alive'
    }

    for key, value in headers.iteritems():
        webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.{}'.format(key)] = value

    webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.userAgent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'

    driver =  webdriver.PhantomJS(*args, **kwargs)
    driver.set_window_size(1400,1000)

    return driver


def main():
    service_args = [
        '--proxy=127.0.0.1:9999',
        '--proxy-type=http',
        '--ignore-ssl-errors=true'
        ]

    driver = init_phantomjs_driver(service_args=service_args)

    driver.get('http://cn.bing.com')

Note: 注意:

userAgent is set in phantomjs.page.settings.userAgent instead of phantomjs.page.customHeaders userAgentphantomjs.page.settings.userAgent设置,而不是phantomjs.page.customHeaders

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM