Skip to content

Commit 91aae73

Browse files
authored
BAEL-8377 | Adding code for avoiding bot detection
Hi , Could you please the following PR related to avoiding bot detection using Selenium!
1 parent 03cf67b commit 91aae73

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.selenium.avoidbotDetectionSelenium;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.Keys;
5+
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.chrome.ChromeDriver;
7+
import org.openqa.selenium.chrome.ChromeOptions;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class AvoidBotDetectionSelenium {
12+
13+
public static void main(String[] args) {
14+
ChromeOptions options = new ChromeOptions();
15+
options.addArguments("--disable-blink-features=AutomationControlled");
16+
17+
ChromeDriver driver = new ChromeDriver(options);
18+
Map<String, Object> params = new HashMap<String, Object>();
19+
params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })");
20+
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);
21+
driver.get("https://www.google.com");
22+
System.out.println("Navigated to Google's homepage.");
23+
24+
WebElement searchBox = driver.findElement(By.name("q"));
25+
System.out.println("Found the search box.");
26+
27+
searchBox.sendKeys("baeldung");
28+
System.out.println("Entered 'baeldung' into the search box.");
29+
30+
searchBox.sendKeys(Keys.ENTER);
31+
System.out.println("Submitted the search query.");
32+
System.out.println("Page title is: " + driver.getTitle());
33+
34+
driver.quit();
35+
}
36+
}

0 commit comments

Comments
 (0)