-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProxyDemo.java
More file actions
73 lines (58 loc) · 2.44 KB
/
ProxyDemo.java
File metadata and controls
73 lines (58 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import io.github.bonigarcia.wdm.WebDriverManager;
import net.lightbody.bmp.BrowserMobProxyServer;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ProxyDemo {
public static void main(String[] args) {
randomIPDemo();
countrySpecificIPDemo("DE");
}
private static void countrySpecificIPDemo(String countryCode) {
WebDriverManager.chromedriver().setup();
BrowserMobProxyServer proxy = ProxyHelper.getProxy(
ProxySetup.ENDPOINT,
ProxySetup.USERNAME,
ProxySetup.PASSWORD,
countryCode); // Returns random proxy in that country
WebDriver driver = ProxyHelper.getDriver(proxy, true);
try {
// Selenium Code Here
driver.get("https://ip.oxylabs.io/");
// Parse the response and get IP from <pre>
try {
WebElement preElement = driver.findElement(By.cssSelector("pre"));
System.out.println("Your IP is " + preElement.getText());
} catch (NoSuchElementException e) {
System.out.println("IP Not Found.\n" + driver.getPageSource());
}
} finally {
driver.quit(); // Quit the driver
proxy.stop(); // Quit local JVM proxy
}
}
private static void randomIPDemo() {
WebDriverManager.chromedriver().setup();
BrowserMobProxyServer proxy = ProxyHelper.getProxy(
ProxySetup.ENDPOINT,
ProxySetup.USERNAME,
ProxySetup.PASSWORD,
null); // Return random proxy in any country
WebDriver driver = ProxyHelper.getDriver(proxy, true);
try {
// Selenium Code Here
driver.get("https://ip.oxylabs.io/");
// Parse the response and get IP from <pre>
try {
WebElement preElement = driver.findElement(By.cssSelector("pre"));
System.out.println("Your IP is " + preElement.getText());
} catch (NoSuchElementException e) {
System.out.println("IP Not Found.\n" + driver.getPageSource());
}
} finally {
driver.quit(); // Quit the driver
proxy.stop(); // Quit local JVM proxy
}
}
}