Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

python - 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. 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? i don't care where: inside ghostdriver, phantomjs or phantomjs-webdriver.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

  • Clone PhantomJS repository
  • Clone ghostdriver repository
  • copy ghostdriver/src/* to phantomjs/src/ghostdriver recursively
  • build 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"
  }
 ...

UPDATE

Use PhantomJS 1.9.2+.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...