栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Selenium Webdriver:修改navigator.webdriver标志以防止selenium检测

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Selenium Webdriver:修改navigator.webdriver标志以防止selenium检测

首先更新1

execute_cdp_cmd()
:随着
execute_cdp_cmd(cmd,cmd_args)
命令的可用性,现在您可以使用Selenium轻松执行google-chrome-
devtools
命令。使用此功能,您可以轻松修改,以防止检测到Selenium
navigator.webdriver


防止检测 2

为了防止检测到Selenium驱动的 WebDriver ,利基方法将包括以下所有步骤之一或全部:

  • 通过命令旋转用户代理
    execute_cdp_cmd()
    ,如下所示:
        #Setting up Chrome/83.0.4103.53 as useragent    driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
  • 将for webdriver*属性 值更改为 undefined
    navigator
    ***
        driver.execute_cdp_cmd("Page.addscriptToevaluateOnNewdocument", {      "source": """        Object.defineProperty(navigator, 'webdriver', {          get: () => undefined        })      """    })
  • 排除

    enable-automation
    开关的集合

    options.add_experimental_option("excludeSwitches", ["enable-automation"])
  • 关掉

    useAutomationExtension

    options.add_experimental_option('useAutomationExtension', False)

示例代码3

汇总上述所有步骤和有效的代码块,将是:

    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, executable_path=r'C:WebDriverschromedriver.exe')    driver.execute_cdp_cmd("Page.addscriptToevaluateOnNewdocument", {      "source": """        Object.defineProperty(navigator, 'webdriver', {          get: () => undefined        })      """    })    driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})    print(driver.execute_script("return navigator.userAgent;"))    driver.get('https://www.httpbin.org/headers')

历史

根据 W3C编辑器草案 ,当前的实现严格提到:

用户代理远程控制 时,该 标志 设置为,最初设置为。

webdriver-active
true
__
false

进一步,

Navigator includes NavigatorAutomationInformation;

要注意的是:

NavigatorAutomationInformation
接口 不应在 WorkerNavigator公开

NavigatorAutomationInformation
接口 定义为:

interface mixin NavigatorAutomationInformation {    readonly attribute boolean webdriver;};

true
如果设置了
webdriver-active
标志, 则返回,否则返回false。

最后,

navigator.webdriver
定义了一种标准方法,用于使用户代理合作以通知文档它由 WebDriver
控制,以便可以在自动化过程中触发备用代码路径。

警告 :更改/调整上述参数可能会阻止 导航 并获取检测到的 WebDriver 实例。


更新(2019年11月6日)

从当前的实现开始,一种理想的访问网页而不被检测到的理想方法是使用

ChromeOptions()
该类向以下参数添加几个参数:

  • 排除
    enable-automation
    开关的集合
  • 关掉
    useAutomationExtension

通过以下实例

ChromeOptions

  • Java示例:
        System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");    ChromeOptions options = new ChromeOptions();    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));    options.setExperimentalOption("useAutomationExtension", false);    WebDriver driver =  new ChromeDriver(options);    driver.get("https://www.google.com/");
  • Python范例
        from selenium import webdriver    options = webdriver.ChromeOptions()    options.add_experimental_option("excludeSwitches", ["enable-automation"])    options.add_experimental_option('useAutomationExtension', False)    driver = webdriver.Chrome(options=options, executable_path=r'C:pathtochromedriver.exe')    driver.get("https://www.google.com/")

传说

1:仅适用于Selenium的Python客户端。

2:仅适用于Selenium的Python客户端。

3:仅适用于Selenium的Python客户端。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374899.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号