Selenium PhantomJS custom headers in Python

Sumit Jha picture Sumit Jha · Feb 27, 2016 · Viewed 22.7k times · Source

I want to add "custom headers" to Selenium PhantomJS in python. These are the headers I wanna add.

headers = { 'Accept':'*/*',
            'Accept-Encoding':'gzip, deflate, sdch',
            'Accept-Language':'en-US,en;q=0.8',
            'Cache-Control':'max-age=0',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'
          }

This is the code I am working with:

from selenium import webdriver

service_args = [
    '--proxy=127.0.0.1:9999',
    '--proxy-type=socks5',
    ]
driver = webdriver.PhantomJS(service_args=service_args)


driver.set_window_size(1120, 550)
driver.get("https://duckduckgo.com/")
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
print driver.current_url
driver.quit()

How do I modify the code incorporating those custom headers ?

Please help.

Answer

Mithril picture Mithril · Oct 14, 2016

Andriy Ivaneyko's method not work for me (PhantomJS 2.1.1 and Selenium 2.48.0).

I write a full example to set all headers, window size and proxy in 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 1:

userAgent is set in phantomjs.page.settings.userAgent instead of phantomjs.page.customHeaders

Note 2:

Andriy Ivaneyko use enumerate to build DesiredCapabilities.PHANTOMJS, the key is loop index, so the data become:

{
 'browserName': 'phantomjs',
 'javascriptEnabled': True,
 'phantomjs.page.customHeaders.0': 'Accept-Language',
 'phantomjs.page.customHeaders.1': 'Accept-Encoding',
 'phantomjs.page.customHeaders.2': 'Accept',
 'phantomjs.page.customHeaders.3': 'User-Agent',
 'phantomjs.page.customHeaders.4': 'Connection',
 'phantomjs.page.customHeaders.5': 'Cache-Control',
 'platform': 'ANY',
 'version': ''
}

None of header attributes is set correctly.