In this article, you will learn how to set up residential and mobile proxies for testing automation in Selenium.
-
Before you begin, you should know that Selenium supports five programming languages: Java, C#, Python, Ruby, and Javascript (Node). The way to set up a proxy will differ depending on the language you choose.
- Next, you need to select the WebDriver you plan to use. Selenium offers web drivers for all popular browsers (a list of all supported web drivers is here). This instruction will show how to set up a proxy using the Google Chrome Driver as an example.
-
Below are code examples in different languages for the Google Chrome Driver:
- Java:
package chromeScripts;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
public class Example {
public static void main(String[] args) {
String ProxyServer = "proxy.froxy.com";
int ProxyPort = 9000;
String sHttpProxy = ProxyServer + ":" + ProxyPort;
Proxy proxy = new Proxy();
proxy.setHttpProxy(sHttpProxy);
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("PATH TO WEBDRIVER"))
.usingAnyFreePort()
.build();
ChromeOptions options = new ChromeOptions();
options.setCapability("proxy", proxy);
options.merge(options);
WebDriver driver=new ChromeDriver(service, options);
driver.get("http://froxy.com/api/detect-ip/");
WebElement body = driver.findElement(By.tagName("body"));
String bodyText = body.getText();
System.out.println(bodyText);
}
}- C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class Program
{
static void Main(string[] args)
{
ChromeOptions options = new ChromeOptions();
var proxy = new Proxy
{
Kind = ProxyKind.Manual,
IsAutoDetect = false,
HttpProxy = "http://proxy.froxy.com:9000"
};
options.Proxy = proxy;
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("http://froxy.com/api/detect-ip/");
var getBody = driver.FindElement(By.TagName("body"));
var getBodyText = getBody.Text;
Console.WriteLine(getBodyText);
Console.WriteLine("Click any key to exit..");
Console.ReadKey();
driver.Quit();
}
}- Python:
HOSTNAME = ''
PORT = ''
### DRIVER
"""
Define the driver as follows:
Firefox:
DRIVER = 'FIREFOX'
Chrome:
DRIVER = 'CHROME'
"""
DRIVER = ''
### DRIVER PATH
"""
Make sure that this exact part is configured with exact location of your webdriver/webdrivers.
The location should link to either geckodriver.exe or chromedriver.exe on Windows.
On MacOS/Linux Systems it should link to chromedriver/geckodriver.
Make sure that DRIVER_PATH is linking to the according Webdriver that you specify below. Example:
DRIVER_PATH = "C:/bin/geckodriver.exe"
"""
DRIVER_PATH = ''
"""
Make sure to not edit settings beyond this point unless you know what you are doing!
"""
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
def get_driver_settings():
DRIVER_SETTINGS = {}
DRIVER_SETTINGS['DRIVER'] = DRIVER
DRIVER_SETTINGS['DRIVER_PATH'] = DRIVER_PATH
return DRIVER_SETTINGS
def froxy():
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = '{hostname}:{port}'.format(hostname = HOSTNAME, port = PORT)
prox.ssl_proxy = '{hostname}:{port}'.format(hostname = HOSTNAME, port = PORT)
if DRIVER == 'FIREFOX':
capabilities = webdriver.DesiredCapabilities.FIREFOX
elif DRIVER == 'CHROME':
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
return capabilities- Ruby:
require 'rubygems'
require 'selenium-webdriver'
proxy = Selenium::WebDriver::Proxy.new(:http => "proxy.froxy.com:9000")
driver = Selenium::WebDriver.for :chrome, :proxy => proxy
driver.navigate.to "http://froxy.com/api/detect-ip/"
element = driver.find_element(:css, 'body')
puts element.text
driver.quit- Javascript:
require('chromedriver');
var webdriver = require('selenium-webdriver'), By = webdriver.By,
until = webdriver.until, chrome = require("selenium-webdriver/chrome");
let addr = 'proxy.froxy.com:9000'
let opt = new chrome.Options().addArguments(`--proxy-server=http://${addr}`)
var driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(opt)
.build();
driver.get('http://froxy.com/api/detect-ip/')
var textPromise = driver.findElement(webdriver.By.css('body')).getText();
textPromise.then((text) => {
console.log(text);
}); -
You can run all these commands in the console.
-
To set up a proxy connection, use proxy.froxy.com:9000 with the correct punctuation (''). Depending on your language of choice, the punctuation may differ:
- Java:
String ProxyServer = "proxy.froxy.com";
int ProxyPort = 9000; - C#:
HttpProxy = "proxy.froxy.com:9000"
- Python:
HOSTNAME = 'proxy.froxy.com'
PORT = '9000' - Ruby:
proxy = Selenium::WebDriver::Proxy.new(http: "proxy.froxy.com:9000")
- Javascript (Node):
let addr = 'proxy.froxy.com:9000'
All necessary information about the server, port, login, and password is available in your Froxy Dashboard.
- Java:
If you still have any questions about the operation of our service froxy.com, you can ask them in the online chat in the lower right corner of the site or contact support at support@froxy.com.