Selenium PhantomJS custom headers in Python
Asked Answered
P

4

14

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.

Pecten answered 27/2, 2016 at 5:25 Comment(1)
This should help: https://mcmap.net/q/694747/-custom-headers-in-phantomjs-selenium-webdriver.Glyptodont
S
16

Setup headers in next way:

from selenium import webdriver


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'
}

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

Then start work with your driver:

service_args = [
    '--proxy=127.0.0.1:9999',
    '--proxy-type=socks5',
]
driver = webdriver.PhantomJS(service_args=service_args)
# ............... 
Shading answered 27/2, 2016 at 5:36 Comment(0)
R
18

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.

Rosinarosinante answered 14/10, 2016 at 7:42 Comment(2)
This is not working anymore. Tried PhantomJS version 1.9.8 and 2.1.1.Officialese
Worked for me PhantomJS 2.1.1.Phylissphyll
S
16

Setup headers in next way:

from selenium import webdriver


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'
}

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

Then start work with your driver:

service_args = [
    '--proxy=127.0.0.1:9999',
    '--proxy-type=socks5',
]
driver = webdriver.PhantomJS(service_args=service_args)
# ............... 
Shading answered 27/2, 2016 at 5:36 Comment(0)
K
1
from selenium import webdriver

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' }

for key in headers:
    webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.{}'.format(key)] = headers[key]
Kellykellyann answered 24/6, 2016 at 12:46 Comment(0)
F
0
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
"(KHTML, like Gecko) Chrome/15.0.87")

driver = webdriver.PhantomJS(desired_capabilities=dcap)
driver.get("http://www.google.com")
Franckot answered 12/6, 2018 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.