防止selenium和ChromeDriver的JS检测java版处理

星期一, 2020-07-27 | Author: Lee | JAVA-and-J2EE, linux | 4,442 views

启用最新版本的 selenium-java的4.0.0-alpha-X版本即可支持CdpCommand抵挡 webdriver的检测为true

maven如下:

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0-alpha-6</version>
</dependency>
<dependency>
     <groupId>com.google.guava</groupId>
     <artifactId>guava</artifactId>
</dependency>

对应java代码如下

System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);
ChromeOptions options = new ChromeOptions();
options.setHeadless(false);
// option.addArguments("--user-agent=" + userAgent);
options.addArguments("--disable-blink-features");
options.addArguments("--disable-blink-features=AutomationControlled");
 
ChromeDriver driver = new ChromeDriver(options);
Map<String, Object> params = new HashMap<String, Object>();
params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })");
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 
//driver.get("https://www.pomelolee.com");

Tags:

文章作者: Lee

本文地址: https://www.pomelolee.com/2088.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Links

Meta