|
| 1 | +# Oxylabs’ Residential Proxies integration with Selenium using Java |
| 2 | + |
| 3 | +[<img src="https://img.shields.io/static/v1?label=&message=Java&color=brightgreen" />](https://github.com/topics/java) [<img src="https://img.shields.io/static/v1?label=&message=Selenium&color=orange" />](https://github.com/topics/selenium) [<img src="https://img.shields.io/static/v1?label=&message=Web-Scraping&color=yellow" />](https://github.com/topics/web-scraping) [<img src="https://img.shields.io/static/v1?label=&message=Rotating%20Proxies&color=blueviolet" />](https://github.com/topics/rotating-proxies) |
| 4 | + |
| 5 | +- [Introduction](#introduction) |
| 6 | +- [Prerequisites](#prerequisites) |
| 7 | +- [Requirements](#requirements) |
| 8 | +- [Running the Code](#running-the-code) |
| 9 | + - [Compiling the Source Code](#compiling-the-source-code) |
| 10 | + - [Running the Jar](#running-the-jar) |
| 11 | +- [Proxy Authentication](#proxy-authentication) |
| 12 | +- [Testing Proxy Connection](#testing-proxy-connection) |
| 13 | +- [Getting Country Specific Proxy](#getting-country-specific-proxy) |
| 14 | +- [SSL Support](#ssl-support) |
| 15 | + - [Installation Certificate for macOS](#installation-certificate-for-macos) |
| 16 | + - [Installation Certificate for Windows](#installation-certificate-for-windows) |
| 17 | +- [Understanding the Code](#understanding-the-code) |
| 18 | + |
| 19 | +## Introduction |
| 20 | + |
| 21 | +Integrating proxies that need authorization using the Selenium framework and Java programming |
| 22 | +language can be challenging. |
| 23 | + |
| 24 | +This tutorial contains complete code demonstrating how [Oxylabs’ Residential Proxies](https://oxylabs.io/products/residential-proxy-pool) can be |
| 25 | +easily integrated with Selenium using Java. |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +- Download and install [Maven](https://maven.apache.org/download.cgi). |
| 30 | +- Download and install [Java SE Development Kit](https://www.oracle.com/java/technologies/downloads/). |
| 31 | +- Download and install [Google Chrome](https://www.google.com/chrome). |
| 32 | + |
| 33 | +## Requirements |
| 34 | + |
| 35 | +To make this integration easier, we used [BrowserMob Proxy](https://github.com/lightbody/browsermob-proxy) |
| 36 | +as a middle layer. It runs proxies locally in JVM and allows chaining of Oxylabs' authenticated proxies. |
| 37 | +If you’re using Maven, add this dependency to the `pom.xml` file: |
| 38 | + |
| 39 | +```xml |
| 40 | + |
| 41 | +<dependency> |
| 42 | + <groupId>net.lightbody.bmp</groupId> |
| 43 | + <artifactId>browsermob-core</artifactId> |
| 44 | + <version>2.1.5</version> |
| 45 | +</dependency> |
| 46 | +``` |
| 47 | + |
| 48 | +The other library used in this project – [WebDriverManager](https://github.com/bonigarcia/webdrivermanager), |
| 49 | +is optional. It just makes downloading and setting up [Chrome Driver](https://chromedriver.chromium.org/downloads) |
| 50 | +easier. To use this library, include the following dependency in `pom.xml` file: |
| 51 | + |
| 52 | +```xml |
| 53 | + |
| 54 | +<dependency> |
| 55 | + <groupId>io.github.bonigarcia</groupId> |
| 56 | + <artifactId>webdrivermanager</artifactId> |
| 57 | + <version>5.0.2</version> |
| 58 | +</dependency> |
| 59 | +``` |
| 60 | + |
| 61 | +If you don’t want to use WebDriverManager, download the Chrome Driver and set the system property as follows: |
| 62 | + |
| 63 | +```java |
| 64 | +System.setProperty("webdriver.chrome.driver","/path/to/chromedriver"); |
| 65 | +``` |
| 66 | + |
| 67 | +## Running the Code |
| 68 | + |
| 69 | +### Compiling the Source Code |
| 70 | + |
| 71 | +This is a maven project. To compile this project, run the following command from terminal: |
| 72 | + |
| 73 | +```shell |
| 74 | +mvn clean package |
| 75 | +``` |
| 76 | + |
| 77 | +This will create the file `oxylabs.io-jar-with-dependencies.jar` in the `target` folder. |
| 78 | + |
| 79 | +### Running the Jar |
| 80 | + |
| 81 | +To run the jar, execute the following command from the terminal: |
| 82 | + |
| 83 | +```shell |
| 84 | +java -cp target/oxylabs.io-jar-with-dependencies.jar ProxyDemo |
| 85 | +``` |
| 86 | + |
| 87 | +## Proxy Authentication |
| 88 | + |
| 89 | +Open [ProxySetup.java](src/main/java/ProxySetup.java) file and update your username, password, and endpoint. |
| 90 | + |
| 91 | +```java |
| 92 | +static final String ENDPOINT="pr.oxylabs.io:7777"; |
| 93 | +static final String USERNAME="yourUsername"; |
| 94 | +static final String PASSWORD="yourPassword"; |
| 95 | +``` |
| 96 | + |
| 97 | +You shouldn’t include the prefix `customer-` in the `USERNAME.` This will be added in the code for country-specific proxies. |
| 98 | + |
| 99 | +## Testing Proxy Connection |
| 100 | + |
| 101 | +Open this project in IDE, open the [ProxySetup.java](src/main/java/ProxySetup.java) file, and run the `main()` function. |
| 102 | +This will print two IP addresses. |
| 103 | + |
| 104 | +- The first IP address will be completely random; |
| 105 | +- The second IP address will be a country-specific IP address in Germany. |
| 106 | + |
| 107 | +## Getting Country Specific Proxy |
| 108 | + |
| 109 | +Open [ProxyDemo.java](src/main/java/ProxyDemo.java) file and send a two-letter country code to the function `CountrySpecficIPDemo`. |
| 110 | + |
| 111 | +```java |
| 112 | +countrySpecificIPDemo("DE"); |
| 113 | +``` |
| 114 | + |
| 115 | +The value of this parameter is a case-insensitive country code in two-letter [3166-1 alpha-2 format](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). For example, `DE` for |
| 116 | +Germany, `GB` for the United Kingdom, etc. For more details, see Oxylabs’ [documentation](https://developers.oxylabs.io/residential-proxies/?java#select-country). |
| 117 | + |
| 118 | +## SSL Support |
| 119 | + |
| 120 | +This code uses BrowserMob Proxy, which supports full MITM. |
| 121 | + |
| 122 | +You may still see invalid certificates warnings. To solve this, install the [ca-certificate-rsa.cer](certificates/ca-certificate-rsa.cer) file in your browser or HTTP client. Alternatively you can generate your own private key rather than using the .cer files distributed with repository. |
| 123 | + |
| 124 | +### Installation Certificate for macOS |
| 125 | +- Open Keychain Access |
| 126 | +- Selct `System` on left bar |
| 127 | +- Click the padlock icon next to `System` and enter your password when prompted |
| 128 | +- Select `Certificates` tab on the top |
| 129 | +- Drag and drop the `ca-certificate-rsa.cer` file into the `Certificates` tab. You will notice a new certificate appears in the `Certificates` tab with the name `LittleProxy MITM`. |
| 130 | + |
| 131 | + |
| 132 | +- Right click the certificate and select `Get Info` |
| 133 | +- Select `Always Trust`, close the dialog, and enter password again when promoted |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +### Installation Certificate for Windows |
| 140 | + |
| 141 | +- Open the `ca-certificate-rsa.cer` file in Windows Explorer. |
| 142 | +- Right click the file, and select `Install`. |
| 143 | +- In the Certificate Import Wizard window, click `Browse` and select `Trusted Publishers`. Click `OK` to continue. |
| 144 | + |
| 145 | + |
| 146 | +- If you see a Security Warning, select `Yes`. |
| 147 | + |
| 148 | + |
| 149 | +- Follow the wizard to complete the installation. |
| 150 | + |
| 151 | +## Understanding the Code |
| 152 | + |
| 153 | +All the complexity of setting up BrowserMob Proxy and Chrome Options is hidden in |
| 154 | +the [ProxyHelper](src/main/java/ProxyHelper.java) class. |
| 155 | + |
| 156 | +In most cases, you should be able to use this file directly without any change. |
| 157 | + |
| 158 | +To create a Chrome Driver instance, go through a two-step process as follows: |
| 159 | + |
| 160 | +First, create an instance of `BrowserMobProxyServer`. This is where you need to provide the proxy endpoint, username, and password. |
| 161 | + |
| 162 | +The fourth parameter is a two-letter country code. If you don’t need a country-specific proxy, set it to `null`: |
| 163 | + |
| 164 | +```java |
| 165 | +BrowserMobProxyServer proxy=ProxyHelper.getProxy( |
| 166 | + ProxySetup.ENDPOINT, |
| 167 | + ProxySetup.USERNAME, |
| 168 | + ProxySetup.PASSWORD, |
| 169 | + countryCode) |
| 170 | +``` |
| 171 | + |
| 172 | +Next, call the `ProxyHelper.getDriver()` function. |
| 173 | + |
| 174 | +This function takes up two parameters -`BrowserMobProxyServer` and a `boolean` headless. To run the browser in headless mode, send `true`: |
| 175 | + |
| 176 | +```java |
| 177 | +WebDriver driver=ProxyHelper.getDriver(proxy,true); |
| 178 | +``` |
| 179 | + |
| 180 | +`driver` is an instance of Chrome Driver. |
| 181 | + |
| 182 | +Now, you should write your code to use the Chrome Driver. |
| 183 | + |
| 184 | +Before exiting, remember to close the driver and stop the proxy: |
| 185 | + |
| 186 | +```java |
| 187 | +driver.quit(); |
| 188 | +proxy.stop(); |
| 189 | +``` |
| 190 | + |
| 191 | +If you're having any trouble integrating Oxylabs’ Residential Proxies with Selenium and this guide didn't help you – feel free to contact our customer support at [email protected]. |
0 commit comments