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

新手上路,请多包涵

我正在尝试使用 selenium 和 chrome 在网站中自动执行一项非常基本的任务,但网站会以某种方式检测到 chrome 何时由 selenium 驱动并阻止每个请求。我怀疑该网站依赖于像这样的公开 DOM 变量 https://stackoverflow.com/a/41904453/648236 来检测硒驱动的浏览器。

我的问题是,有没有办法让 navigator.webdriver 标志为 false?我愿意在修改后尝试重新编译硒源,但我似乎无法在存储库 https://github.com/SeleniumHQ/selenium 的任何地方找到 NavigatorAutomationInformation 源

任何帮助深表感谢

PS:我还从 https://w3c.github.io/webdriver/#interface 尝试了以下内容

Object.defineProperty(navigator, 'webdriver', {
    get: () => false,
  });

但它只在初始页面加载后更新属性。我认为该站点在我的脚本执行之前检测到变量。

原文由 Ajanth 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.9k
2 个回答

首先是更新1

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


防止检测2

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

   from selenium import webdriver

  options = webdriver.ChromeOptions()
  options.add_argument('--disable-blink-features=AutomationControlled')
  driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
  driver.get("https://www.website.com")

你可以在 Selenium can’t open a second page 中找到相关的详细讨论

  • 通过 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'})

  • webdrivernavigator属性 值更改为 undefined
   driver.execute_script("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:\WebDrivers\chromedriver.exe')
driver.execute_script("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 Editor’s Draft ,当前的实现严格提到:

webdriver-active 标志 设置为 true用户代理 处于 远程控制 下时,最初设置为 false

更远,

 Navigator includes NavigatorAutomationInformation;

需要注意的是:

不应在 WorkerNavigator 上公开 NavigatorAutomationInformation _接口_。

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:\path\to\chromedriver.exe')
  driver.get("https://www.google.com/")

  • 红宝石示例
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument("--disable-blink-features=AutomationControlled")
    driver = Selenium::WebDriver.for :chrome, options: options


传说

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

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

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

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 4.0 许可协议

铬驱动程序

终于用一个简单的标志找到了解决这个问题的简单方法! :)

 --disable-blink-features=AutomationControlled

navigator.webdriver=true 将不再显示该标志集。

有关您可以禁用的内容列表, 请在此处查看

原文由 010011100101 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题