使用Selenium运行无头Firefox报错'Options'对象无'binary'属性怎么办
错误原因与修正方案
- 报错的核心原因是浏览器配置类与驱动不匹配:你导入的是Chrome专属的
Options配置类,却用来初始化Firefox驱动,两类浏览器的配置属性不兼容,才会触发'Options' object has no attribute 'binary'的错误。
方案1:沿用Firefox浏览器运行
替换为Firefox专属的Options类即可,高版本Selenium同步调整元素定位写法:
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.common.by import By options = Options() options.add_argument("--headless") driver = webdriver.Firefox(options=options) driver.get('https://www.walmart.com/ip/Sony-PlayStation-5-Video-Game-Console/994712501') print(driver.find_element(By.CSS_SELECTOR, "div[class='prod-blitz-copy-message']").text)
方案2:切换为Chrome浏览器运行
如果你的环境已配置好Chrome及对应版本的chromedriver,可改为Chrome驱动:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By options = Options() # Chrome 112及以上版本推荐使用新无头模式参数,旧版本可替换为--headless options.add_argument("--headless=new") driver = webdriver.Chrome(options=options) driver.get('https://www.walmart.com/ip/Sony-PlayStation-5-Video-Game-Console/994712501') print(driver.find_element(By.CSS_SELECTOR, "div[class='prod-blitz-copy-message']").text)
- 额外注意:沃尔玛平台存在反爬策略,高频访问会触发人机验证,可通过添加请求头UA、设置代理等方式降低拦截概率;运行前请确认浏览器驱动版本与本地安装的浏览器版本完全匹配,避免驱动初始化失败。
内容的提问来源于stack exchange,提问作者Lenue001
相关产品推荐
相关产品推荐

