Selenium反屏蔽处理
Selenium自动化过程,在浏览器内会显示如下字样
当出现此内容时,有些网站就会判定是机器在进行操作,然后网站会加载防机器操作程序,如下图滑块验证
触发反机器操作的原理大概如下
解决方法
具体代码,加入到脚本中,替代定义浏览器即可
option = webdriver.ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-automation']) option.add_experimental_option('useAutomationExtension', False) browser = webdriver.Chrome(options=option) browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', { 'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})' })
执行后就不会有显示浏览器正受到自动软件的控制,也没有验证码了
from selenium import webdriver from selenium.webdriver.common.keys import Keys from webdriver_manager.chrome import ChromeDriverManager import time option = webdriver.ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-automation']) option.add_experimental_option('useAutomationExtension', False) browser = webdriver.Chrome(options=option) browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', { 'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})' }) #browser = webdriver.Chrome() #browser = webdriver.Chrome(ChromeDriverManager().install()) browser.get('http://www.baidu.com') search = browser.find_element_by_id('kw') search.send_keys('python') search.send_keys(Keys.ENTER) time.sleep(20) #browser.close()