How to set Network Preference parameters for headless browser PhantomJS with Python and Selenium

265 Views Asked by At

How to implement below profile set preference for Phantomjs headless browser.

def browser():
    print("Setting up webdriver connection...")
    global driver
    if driver is None:
        profile = webdriver.FirefoxProfile()
        profile.accept_untrusted_certs = True
        profile.set_preference("network.proxy.type",1)
        profile.set_preference("network.proxy.http","web-proxy.xxxx.xx.com")
        profile.set_preference("network.proxy.https","web-proxy.xxxx.xx.com")
        profile.set_preference("network.proxy.ssl","web-proxy.xxxx.xx.com")
        profile.set_preference("network.proxy.http_port",8080)
        profile.set_preference("network.proxy.https_port",8080)
        profile.set_preference("network.proxy.ssl_port",8080)
        profile.update_preferences()
        driver = webdriver.Firefox(firefox_profile=profile)
        driver.maximize_window()
        yield driver
        driver.quit()

I am using python 2.7, selenium 3.0 and phantomjs 1.9.7.

1

There are 1 best solutions below

0
undetected Selenium On

To set the Network Preferences for PhantomJS Headless Browser you can use the argument service_args as follows :

driver = webdriver.PhantomJS(executable_path="/path/to/phantomjs", service_args=['--proxy-type=[http|socks5|none]', '--proxy=address:port'])

An Example on Windows OS:

driver = webdriver.PhantomJS(executable_path=r'C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe', service_args=['--proxy-type=http', '--proxy=web-proxy.xxxx.xx.com:8080'])

Note : Consider upgrading to latest Selenium/Python Client v3.12.0 and latest PhantomJS Release v2.1.1