| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Integrating proxies that need authorization using the Selenium framework and Java programming language can be challenging.
This tutorial contains complete code demonstrating how Oxylabs’ Residential Proxies can be easily integrated with Selenium using Java.
To make this integration easier, we used BrowserMob Proxy as a middle layer. It runs proxies locally in JVM and allows chaining of Oxylabs' authenticated proxies. If you’re using Maven, add this dependency to the pom.xml file:
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
</dependency>The other library used in this project – WebDriverManager, is optional. It just makes downloading and setting up Chrome Driver easier. To use this library, include the following dependency in pom.xml file:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.0.2</version>
</dependency>If you don’t want to use WebDriverManager, download the Chrome Driver and set the system property as follows:
System.setProperty("webdriver.chrome.driver","/path/to/chromedriver");This is a maven project. To compile this project, run the following command from terminal:
mvn clean packageThis will create the file oxylabs.io-jar-with-dependencies.jar in the target folder.
To run the jar, execute the following command from the terminal:
java -cp target/oxylabs.io-jar-with-dependencies.jar ProxyDemoOpen ProxySetup.java file and update your username, password, and endpoint.
static final String ENDPOINT="pr.oxylabs.io:7777";
static final String USERNAME="yourUsername";
static final String PASSWORD="yourPassword";You shouldn’t include the prefix customer- in the USERNAME. This will be added in the code for country-specific proxies.
Open this project in IDE, open the ProxySetup.java file, and run the main() function. This will print two IP addresses.
Open ProxyDemo.java file and send a two-letter country code to the function CountrySpecficIPDemo.
countrySpecificIPDemo("DE");The value of this parameter is a case-insensitive country code in two-letter 3166-1 alpha-2 format. For example, DE for Germany, GB for the United Kingdom, etc. For more details, see Oxylabs’ documentation.
This code uses BrowserMob Proxy, which supports full MITM.
You may still see invalid certificates warnings. To solve this, install the 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.
All the complexity of setting up BrowserMob Proxy and Chrome Options is hidden in the ProxyHelper class.
In most cases, you should be able to use this file directly without any change.
To create a Chrome Driver instance, go through a two-step process as follows:
First, create an instance of BrowserMobProxyServer. This is where you need to provide the proxy endpoint, username, and password.
The fourth parameter is a two-letter country code. If you don’t need a country-specific proxy, set it to null:
BrowserMobProxyServer proxy=ProxyHelper.getProxy(
ProxySetup.ENDPOINT,
ProxySetup.USERNAME,
ProxySetup.PASSWORD,
countryCode)Next, call the ProxyHelper.getDriver() function.
This function takes up two parameters -BrowserMobProxyServer and a boolean headless. To run the browser in headless mode, send true:
WebDriver driver=ProxyHelper.getDriver(proxy,true);driver is an instance of Chrome Driver.
Now, you should write your code to use the Chrome Driver.
Before exiting, remember to close the driver and stop the proxy:
driver.quit();
proxy.stop(); 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 support@oxylabs.io.
| Back | FazBrowse Home | New Git URL |