嘗試使用Selenium 和Chrome 自動化任務時,某些網站可能會根據Selenium 的檢測來阻止請求-驅動的瀏覽器。一種常見的偵測方法涉及公開一個名為 navigator.webdriver 的 DOM 變量,如果瀏覽器由 Selenium 控制,則變數將傳回 true。
要規避此檢測,請考慮以下方法:
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"path/to/chromedriver.exe")
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd("Network.setUserAgentOverride", {"userAgent": "new_user_agent"})
options.add_argument("--disable-blink-features=AutomationControlled")
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"path/to/chromedriver.exe") driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") driver.execute_cdp_cmd("Network.setUserAgentOverride", {"userAgent": "new_user_agent"}) driver.get("https://www.example.com")
以上是如何透過修改「navigator.webdriver」標誌來防止 Selenium 偵測?的詳細內容。更多資訊請關注PHP中文網其他相關文章!