Configuring Network Proxy Settings for PhantomJS in Python with Selenium

I need help configuring proxy options for a PhantomJS headless session using Python and Selenium. My goal is to correctly adjust the browser’s network settings, similar to a Firefox profile, to work behind a proxy. I am currently running Python 2.7, Selenium 3.0, and PhantomJS 1.9.7. Below is an example of my revised configuration code:

from selenium import webdriver

def init_driver():
    print('Initializing Selenium driver...')
    global browser_instance
    if browser_instance is None:
        ff_profile = webdriver.FirefoxProfile()
        ff_profile.accept_untrusted_certs = True
        ff_profile.set_preference('network.proxy.mode', 1)
        ff_profile.set_preference('network.proxy.http', 'proxy-server.example.net')
        ff_profile.set_preference('network.proxy.https', 'proxy-server.example.net')
        ff_profile.set_preference('network.proxy.ssl', 'proxy-server.example.net')
        ff_profile.set_preference('network.proxy.http_port', 8080)
        ff_profile.set_preference('network.proxy.https_port', 8080)
        ff_profile.set_preference('network.proxy.ssl_port', 8080)
        ff_profile.update_preferences()
        browser_instance = webdriver.Firefox(firefox_profile=ff_profile)
        browser_instance.maximize_window()
        yield browser_instance
        browser_instance.quit()

hey, phantomjs doesn’t use firefox profiles so you need to use service_args for proxy settings like: service_args=[‘–proxy=proxy-server.example.net:8080’] rather than modifying profile prefs. hope this helps!

After struggling for a while with configuring proxy settings in PhantomJS, I found that the traditional approach using Firefox profiles just doesn’t work due to the way PhantomJS is built. Instead, I had to rely on the command-line arguments passed via service_args to set up the proxy environment properly. In my experience, initializing PhantomJS with service_args like [‘–proxy=proxy-server.example.net:8080’, ‘–proxy-type=http’] consistently resolved the issues I faced. While PhantomJS might suit lightweight tasks, consider exploring headless Chrome or Firefox for more robust proxy support.

PhantomJS’s support for configuring proxy settings is far from optimal, particularly because it does not recognize Firefox profiles. In my experience, the cleanest solution is to pass the necessary proxy details directly through the service_args parameter. Although using service_args like [‘–proxy=proxy-server.example.net:8080’, ‘–proxy-type=http’] is straightforward, make sure to adapt the syntax based on the version you are using. If encountering persistent issues, I recommend considering alternatives such as headless Chrome, which offer better debugging options and an actively maintained API.