Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring proxies on Python + Selenium + Firefox WebDriver

I can't connect using proxies via Selenium Firefox WebDriver.

With this configuration, the connection is generated but NOT via the proxy but the local server.

There are two questions on this matter and this documentation, but none seem to have solved this for python3:

def selenium_connect():

    proxy = "178.20.231.218"
    proxy_port = 80
    url = "https://www.whatsmyip.org/"

    fp = webdriver.FirefoxProfile()
    # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
    fp.set_preference("network.proxy.type", 1)
    fp.set_preference("network.proxy.http",proxy)
    fp.set_preference("network.proxy.http_port",proxy_port)
    fp.update_preferences()
    driver = webdriver.Firefox(firefox_profile=fp)
    driver.get(url)

I'm using Firefox webdriver version 52.0.2 and Python 3.7 and a standard Ubuntu 16.04 Docker environment.

like image 562
Rimo Avatar asked Mar 15 '19 21:03

Rimo


People also ask

Does Selenium act as a proxy server?

The leading web framework, Selenium, may configure your proxy server to support the testing requirements. If Selenium powers the test, then an HTTP request will generate a browser. The HTTP request runs several proxies at a time and will send the localized results.

Which of the following code is used in Selenium to configure the use of proxy?

Following piece of code used to set proxy in Selenium. ChromeOptions option = new ChromeOptions(); Proxy proxy = new Proxy(); proxy. setHttpProxy("localhost:5555"); option. setCapability(CapabilityType.


1 Answers

Don’t you need to set the proxy with DesiredCapabilities and not in a FirefoxProfile? Like the following.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.proxy import Proxy

proxy_to_use= "xxx.xxx.xxx.xxx"
desired_capability = webdriver.DesiredCapabilities.FIREFOX
desired_capability['proxy'] = {
    'proxyType': "manual",
    'httpProxy': proxy_to_use,
    'ftpProxy': proxy_to_use,
    'sslProxy': proxy_to_use
        }
 browser = webdriver.Firefox(capabilities=desired_capability)
 browser.get(“http://www.whatsmyip.org”)
like image 95
C. Peck Avatar answered Oct 05 '22 09:10

C. Peck