Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Phantomjs Selenium WebDriver中的自定义标题_Python_Selenium_Selenium Webdriver_Phantomjs_Ghostdriver - Fatal编程技术网

Python Phantomjs Selenium WebDriver中的自定义标题

Python Phantomjs Selenium WebDriver中的自定义标题,python,selenium,selenium-webdriver,phantomjs,ghostdriver,Python,Selenium,Selenium Webdriver,Phantomjs,Ghostdriver,根据这一点,现在可以修改标题。我需要修改PhantomJS webdriver中的Accept语言。这个代码不起作用 DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Accept-Language'] = 'ru-RU' driver = webdriver.PhantomJS() 是否可以通过某种方式配置Phantomjs来发送我的头文件?我不在乎在哪里:在ghostdriver内部、phantomjs或phantomj

根据这一点,现在可以修改标题。我需要修改PhantomJS webdriver中的Accept语言。这个代码不起作用

DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Accept-Language'] = 'ru-RU'
driver = webdriver.PhantomJS()
是否可以通过某种方式配置Phantomjs来发送我的头文件?我不在乎在哪里:在ghostdriver内部、phantomjs或phantomjs webdriver

PhantomJS的最新版本()于2013年6月5日发布。拉取请求被合并

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

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

  • 克隆PhantomJS存储库
  • 克隆ghostdriver存储库
  • 将ghostdriver/src/*递归复制到phantomjs/src/ghostdriver
  • 构建幻影
使用新构建的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)

更新
使用PhantomJS 1.9.2+。

我编写了一个完整的示例来设置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')
注:
userAgent
是在
phantomjs.page.settings.userAgent
中设置的,而不是在
phantomjs.page.customHeaders

中设置的。谢谢,这对我来说很有效,但不适用于Javascript<代码>打印(driver.execute\u脚本(“return navigator.language”)。它对你有用吗?//@dimazubrik@Neil,这是另一回事。看到奇怪的事了吗。。但是,这对我来说已经不起作用了|
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')