Selenium定位Cookie授权按钮失败,求助解决方案及替代方法
我编写了以下Selenium代码来点击Cookie按钮,因为必须接受Cookie才能访问网站数据:
from selenium import webdriver from selenium.webdriver.common.by import By chromeOptions = webdriver.ChromeOptions() chromeOptions.add_argument("ignore-certificate-errors") chromeOptions.add_argument('--headless=chrome') wbe = webdriver.Chrome(options=chromeOptions) wbe.get('https://www.asianbetsoccer.com/it/nextgame.html') wbe.implicitly_wait(10) wbe.find_element(By.XPATH, "/html/body/button").click()
但运行时出现错误,提示找不到该按钮:
wbe.find_element(By.XPATH, "/html/body/button") File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 741, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute self.error_handler.check_response(response) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/button"} (Session info: chrome-headless-shell=124.0.6367.91); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception Stacktrace: GetHandleVerifier [0x00007FF754EB1502+60802] (No symbol) [0x00007FF754E2AC02] (No symbol) [0x00007FF754CE7CE4] (No symbol) [0x00007FF754D36D4D] (No symbol) [0x00007FF754D36E1C] (No symbol) [0x00007FF754D7CE37] (No symbol) [0x00007FF754D5ABBF] (No symbol) [0x00007FF754D7A224] (No symbol) [0x00007FF754D5A923] (No symbol) [0x00007FF754D28FEC] (No symbol) [0x00007FF754D29C21] GetHandleVerifier [0x00007FF7551B411D+3217821] GetHandleVerifier [0x00007FF7551F60B7+3488055] GetHandleVerifier [0x00007FF7551EF03F+3459263] GetHandleVerifier [0x00007FF754F6B846+823494] (No symbol) [0x00007FF754E35F9F] (No symbol) [0x00007FF754E30EC4] (No symbol) [0x00007FF754E31052] (No symbol) [0x00007FF754E218A4] BaseThreadInitThunk [0x00007FFCEC3E7AC4+20] RtlUserThreadStart [0x00007FFCEDC2A4E1+33]
我是否可以继续使用Selenium解决,还是需要采用其他方法?


可以继续用Selenium解决,以下是调整方案:
抛弃绝对XPATH
绝对路径/html/body/button完全依赖页面结构,只要页面元素层级变化就会失效。改用基于元素文本、class或属性的定位方式,比如针对第一个按钮,用//button[contains(text(), 'Accetta')]或根据它的class属性定位。用显式等待替代隐式等待
隐式等待是全局生效的等待逻辑,显式等待可以针对单个元素等待它可点击,更精准可靠。导入WebDriverWait和expected_conditions来实现。分步处理两次弹窗操作
从图中可以看到,点击第一个按钮后会弹出Cookie授权窗口,需要依次点击"Accetta tutto"(接受全部)和"Salva e continua"(保存并继续)。
修改后的代码示例
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC chromeOptions = webdriver.ChromeOptions() chromeOptions.add_argument("ignore-certificate-errors") chromeOptions.add_argument('--headless=chrome') # 无头模式下指定窗口大小,避免元素因布局问题无法定位 chromeOptions.add_argument('--window-size=1920,1080') # 绕过部分网站的自动化检测 chromeOptions.add_argument('--disable-blink-features=AutomationControlled') wbe = webdriver.Chrome(options=chromeOptions) wbe.get('https://www.asianbetsoccer.com/it/nextgame.html') try: # 等待第一个非弹窗按钮可点击并点击 first_btn = WebDriverWait(wbe, 15).until( EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accetta')]")) ) first_btn.click() # 等待弹窗中的"Accetta tutto"按钮并点击 accept_all_btn = WebDriverWait(wbe, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[text()='Accetta tutto']")) ) accept_all_btn.click() # 等待"Salva e continua"按钮并点击 save_continue_btn = WebDriverWait(wbe, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[text()='Salva e continua']")) ) save_continue_btn.click() # 这里写后续的网站数据获取逻辑 print("Cookie授权完成,可访问网站数据") except Exception as e: print(f"操作出错: {str(e)}") finally: wbe.quit()
额外注意事项
- 如果元素位于iframe内,需要先切换到对应的iframe:
wbe.switch_to.frame("iframe_id"),操作完成后切回主文档:wbe.switch_to.default_content()。 - 可以用浏览器F12开发者工具查看元素的真实属性,确认定位表达式是否正确。
- 若还是出现定位问题,可尝试添加
--user-agent参数模拟真实浏览器请求。
内容的提问来源于stack exchange,提问作者Marco
相关产品推荐
相关产品推荐

