How can I use Selenium with a proxy?

You can use Selenium with a proxy by setting up the WebDriver to use a proxy server for all outgoing HTTP requests made by the browser. Here's how you can do it in Python and JavaScript:

Python:

In Python, you can use the Proxy class from selenium.webdriver.common.proxy to set up a proxy.

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "ip:port"
proxy.ssl_proxy = "ip:port"

capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

driver = webdriver.Chrome(desired_capabilities=capabilities)

Replace "ip:port" with your proxy address and port.

JavaScript:

In JavaScript, you can use the addArguments method to add a proxy to the Chrome WebDriver.

const {Builder} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

let options = new chrome.Options();
options.addArguments('--proxy-server=http://ip:port');

let driver = new Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();

Replace "http://ip:port" with your proxy address and port.

In both Python and JavaScript, the WebDriver will now use the specified proxy server for all HTTP requests.

Note: Make sure your proxy server is up and running when you start your Selenium tests, otherwise, WebDriver may fail to establish a connection.

Console Commands: You have to install Selenium WebDriver for Python and JavaScript:

For Python:

pip install selenium

For JavaScript:

npm install selenium-webdriver

And also, don't forget to install Chrome WebDriver and add it to your system's PATH. You can download it from the official ChromeDriver download page.

For Python, you might also need to install the webdriver_manager to help manage the ChromeDriver binary:

pip install webdriver_manager

You can then use it as follows in your Python script:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

This will automatically download and install the latest version of the Chrome WebDriver binary, and add it to your system's PATH.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon