如何检测Chrome浏览器在硒中是否无头?

Dub*_*Dan 6 python selenium google-chrome selenium-chromedriver selenium-webdriver

我正在编写一个 selenium 测试,考虑到 chrome 浏览器是否以无头方式启动,该测试具有不同的行为。我的问题是在我的测试中如何检测浏览器是否对我的条件流无头?

use*_*789 7

https://antoinevastel.com/bot%20detection/2018/01/17/detect-chrome-headless-v2.html#:~:text=In%20order%20to%20automate%20Chrome,可能%20to%20detect%20Chrome %20无头

driver.execute_script("return navigator.plugins.length == 0")
Run Code Online (Sandbox Code Playgroud)

  • 链接的帖子已更新,现在看来 `driver.execute_script("return navigator.webdriver") is True` 就是这样 (3认同)

小智 -1

当启动 chrome headless 实例时,您需要显式地将参数“--headless”添加到 chromeOptions 对象中。例如,如果您正在为网站编写测试框架,您可能拥有某种浏览器创建器类,该类能够为您提供不同的浏览器来使用。为什么不将该参数保存为该类的附加成员呢?

如果您的代码中没有这种工厂设计,另一个更简单的选择就是

options = webdriver.ChromeOptions
options.add_argument("--headless")
print(options.arguments)
Run Code Online (Sandbox Code Playgroud)