Chrome如何设定webdriver=undefined以避免Selenium检测?

Chrome如何设定webdriver=undefined以避免Selenium检测?

一、WebDriver规范

根据WebDriver规范(https://w3c.github.io/webdriver/#x4-interface)的描述,WebDriver定义了一个标准方法,以便于文档(document)判断当前浏览器处于自动化控制之中。

这个方法就是检测window.navigator.webdriver的值,正常情况下其值为undefined,自动化控制下为true。注意,正常情况下不是false,在JavaScript中undefined为未定义,即该值不存在,而false表示一布尔值。

附上规范原文:

The webdriver-active flag is set to true when the user agent is under remote control. It is initially false.

Defines a standard way for co-operating user agents to inform the document that it is controlled by WebDriver, for example so that alternate code paths can be triggered during automation.

二、突破

ChromeDriver的设计符合这一规范,如何突破

旧版本

版本79.0.3945.16之前,可用如下方法:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)

driver = webdriver.Chrome(options=options)
driver.get("YOUR_URL")
# 在控制台中验证window.navigator.webdriver的值为undefined。
driver.quit()

新版本

版本79.0.3945.16之后,ChromeDriver修正了这一“问题”。

根据注记原文:

Resolved issue 3133: window.navigator.webdriver is undefined when "enable-automation" is excluded in non-headless mode (should be true) [Pri-2]

如何突破

execute_cdp_cmd函数来帮忙!cdp即Chrome DevTools Protocal,Chrome开发者工具协议。

通过该函数在文档加载前注入一段js代码以消去webdriver值。

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from selenium import webdriver

driver = webdriver.Chrome()
script = '''
Object.defineProperty(navigator, 'webdriver', {
    get: () => undefined
})
'''
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": script})
driver.get("YOUR_URL")
# 在控制台中验证window.navigator.webdriver的值为undefined。
driver.quit()

原文地址:https://www.cnblogs.com/bgmc/p/12154484.html

时间: 2024-08-30 14:31:23

Chrome如何设定webdriver=undefined以避免Selenium检测?的相关文章

【win10】selenium之Firefox,Chrome,IE对应webdriver的安装配置

一.安装Python3 1. 可以到Python官方网站:https://www.python.org/downloads/  下载并安装Python,建议安装Python3(由于已经安装过,此处就不继续阐述安装步骤,安装步骤和其他软件一样,安装完成后把安装目录加入到环境变量,可参考其他文章) 2. 安装后,检查是否安装成功的方法: 在Windows命令行(cmd),输入:python,出现python版本信息就表明安装成功 注:若提示Python不是内部或者外部命令,就把Python的安装目录

chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式

WebDriverException: Message: unknown error: Chrome failed to start: crashed 1. 背景在使用selenium + chrome浏览器渲染模式爬取数据时,如果并发任务过多,或者爬虫的运行时间很长,那么很容易出现浏览器崩溃的现象,如下: 这一般是资源消耗过大造成的(据说chrome浏览器有内存泄漏的情况).那如何解决这个问题呢? 这种情况下,我们首先就会想到使用无界面的浏览器PhantomJS,但是PhantomJS现在年久

基于webdriver的jmeter性能测试-Selenium IDE

前言: 由于某些项目使用了WebGL技术,需要高版本的Firefox和Chrome浏览器才能支持浏览,兼容性很弱,导致Loadrunner和jmeter(badboy)无法正常进行录制脚本.因此我们采用selenium IDE录制场景后转换为JUnit4,通过eclipse编译后导出jar文件,再在jmeter中使用JUnit Request控件进行测试 前置条件: JAVA环境: C:\Users\USER>java -version java version "1.8.0_102&qu

WebDriver:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms

今天尝试最新的webDriver与fireFox搭配: 运行代码时出现如下的问题,但是浏览器却可以打开: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewPro

使用selenium 检测js报错

背景:接到一个需求,想检测页面是否能检测js报错,何为js报错,如下图所示,在控制台中,使用console,如果有js报错,就会出现错误 如何检测,简单版操作,打开一个url,使用manage获取浏览器的日志,这样会打印出这个页面获取的内容 @Test public void test2(){ op.loopGet("https://www.rosewholesale.com/cheapest/chic-embossing-braid-5-pieces-3027854.html", 5

Selenium之Chrome浏览器的启动

1.下载Chromedriver.exe文件放至需要的目录中: 2.编写代码 import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; /** * Created by Administrator on 2018/3/29 0029. */ public class ChromeTest { public static void main(String[] args){ WebDri

关于在selenium 中 webdriver 截图操作

package prictce; import java.io.File; import java.io.IOException; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.We

设置Webdriver启动chrome为默认用户的配置信息

Webdriver 启动Chrome浏览器时,默认是打开一个新用户,而非默认用户,即新用户没有我们安装扩展程序.但在实际应用中,我们会需要 默认用户安装的一些扩展程序,比如对于某些js或者css样式,需要代理才能访问成功,使用默认用户就显得尤为重要(因为你不可能在新用户在安装扩展程序再继续测试). 如图: a)默认用户的扩展: 在锁定chrome的任务栏打开的状态: b) WebDriver打开的新用户的扩展: 在锁定chrome的任务栏打开的状态: ----------------------

java selenium webdriver处理JS操作窗口滚动条

未经作者允许,禁止转载!!! import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class JS20161212 { public static void main(String[] args) throws InterruptedException { // TODO