'Proxy: Selenium + Python + Firefox

I'm trying to set up a proxy for Selenium and Firefox (using Python). I have seen many tutorials and posts how to do it, but most are outdated or don't work for me.

I've been trying to use:

ua = UserAgent()
    userAgent = ua.random

    options = Options()
    options.set_preference('general.useragent.override', userAgent)

    options.set_preference('network.proxy.type', 1)
    options.set_preference('network.proxy.http', proxy['ip'])
    options.set_preference('network.proxy.http_port', proxy['port'])
    options.set_preference('network.proxy.ssl', proxy['ip'])
    options.set_preference('network.proxy.ssl_port', proxy['port'])
    options.set_preference('network.proxy.socks_remote_dns', False)

    service = Service(executable_path='../geckodriver-v0.30.0-linux64')
    driver = webdriver.Firefox(service=service, options=options)
    driver.get('https://www.expressvpn.com/what-is-my-ip')

The website https://www.expressvpn.com/what-is-my-ip shows my home IP, so proxying doesn't work.

I also tried:

ua = UserAgent()
    userAgent = ua.random

    options = Options()
    options.set_preference('general.useragent.override', userAgent)

    str_proxy = f"{proxy['ip']}:{proxy['port']}"
    
    my_proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': str_proxy,
        'ftpProxy': str_proxy,
        'sslProxy': str_proxy,
    })

    service = Service(executable_path='../geckodriver-v0.30.0-linux64')
    driver = webdriver.Firefox(service=service, options=options, proxy=my_proxy)
    driver.get('https://www.expressvpn.com/what-is-my-ip')

I also tried:

ua = UserAgent()
    userAgent = ua.random

    options = Options()
    options.page_load_strategy = 'eager'
    options.set_preference('general.useragent.override', userAgent)

    str_proxy = f"{proxy['ip']}:{proxy['port']}"

    webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
        "httpProxy": str_proxy,
        "ftpProxy": str_proxy,
        "sslProxy": str_proxy,
        "proxyType": "MANUAL",

    }

    service = Service(executable_path='../geckodriver-v0.30.0-linux64')
    driver = webdriver.Firefox(service=service, options=options)
    driver.get('https://www.expressvpn.com/what-is-my-ip')

The str_proxy is something like: 8.214.41.50:80

All of the above ways don't work. So, how do I use proxy with Selenium 4 python and Firefox?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source