透過 Selenium 和 Chrome 實現日常網站功能自動化時,Selenium 偵測提出了重大挑戰。有些網站會主動檢查 Selenium 驅動的瀏覽器,阻止某些要求。通常,它們依賴暴露的 DOM 變數(如 navigator.webdriver)來偵測此類實例。
要阻止這種檢測機制,請考慮以下預防措施:
修改您的Selenium 腳本以將特定參數注入Chrome 實例。這包括:
停用「AutomationControlled」標誌:
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--disable-blink-features=AutomationControlled') driver = webdriver.Chrome(options=options, executable_path=path_to_driver)
設定自訂使用者代理:
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Your_Custom_User_Agent'})
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
排除開關🎜>
options.add_experimental_option("excludeSwitches", ["enable-automation"])options.add_experimental_option('useAutomationExtension', False)
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=path_to_driver) driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Your_Custom_User_Agent'}) print(driver.execute_script("return navigator.userAgent;")) driver.get('https://www.httpbin.org/headers')
以上是如何透過修改 Navigator.webdriver 標誌來防止 Selenium 偵測?的詳細內容。更多資訊請關注PHP中文網其他相關文章!