Selenium使用Chrome webdriver访问网站提示Access denied问题求助
问题原因
该站点的反爬虫机制识别到了Selenium原生ChromeDriver的自动化特征,在请求阶段就直接返回了访问拦截,和后续的元素定位、账号输入逻辑无关。
原生Selenium启动的Chrome会暴露大量可被检测的特征,比如window.navigator.webdriver属性值为true、存在cdc_前缀的全局变量、启动参数带自动化标记等,反爬系统匹配到这些特征后就会直接返回Access Denied。
可行解决方案
- 替换原生Selenium为
undetected-chromedriver,该工具专门针对主流反爬的WebDriver特征检测做了隐藏处理,安装命令:pip install undetected-chromedriver - 配置Chrome启动参数,抹除自动化标识,关键参数如下:
- 关闭自动化控制特征检测:
--disable-blink-features=AutomationControlled - 排除自动化开关标记:
--excludeSwitches=enable-automation - 禁用自动化扩展:
--useAutomationExtension=false - 配置和正常浏览器一致的UA标识:
--user-agent=你本地正常使用的Chrome浏览器的UA字符串
- 关闭自动化控制特征检测:
- 避免短时间内高频访问站点,防止IP被临时封禁,若持续拦截可更换网络环境或使用代理IP。
修改后可运行的参考代码
import undetected_chromedriver as uc from selenium.webdriver.common.by import By import time # 可根据需求自定义启动参数 options = uc.ChromeOptions() options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36') driver = uc.Chrome(options=options) driver.get("https://www.lowes.com/login") time.sleep(5) driver.find_element(By.ID, "Email").send_keys("teshooabood@gmail.com") driver.find_element(By.ID, "Password").send_keys("Tz5PRZPeF@ytb3e\n")
注意:新版Selenium已经废弃find_element_by_id写法,建议改用上述By.ID的写法,避免运行报错。
内容的提问来源于stack exchange,提问作者Teshoo Abood
相关产品推荐
相关产品推荐

