Python Selenium无法定位并点击u.gg的Cookie同意按钮求助
代码核心问题
By.CLASS_NAME不支持传入多个类名:你写的"fc-button fc-cta-consent fc-primary-button"是多个类,By.CLASS_NAME仅能识别单个类,导致找不到按钮元素。- 点击方法未执行:
button.click少了括号,正确写法是button.click(),否则不会触发点击动作。 - 固定
time.sleep不稳定:容易出现元素未加载完成就执行操作的情况。
修复后代码(正确实现按钮点击)
改用显式等待+CSS选择器定位按钮,确保元素可点击后再执行操作:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC url = "https://u.gg/lol/champions/kayle/build" options = Options() options.add_argument("--headless") options.add_argument("--start-maximized") # 可选:添加User-Agent,避免被识别为无头浏览器 options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") service = Service("chromedriver.exe") driver = webdriver.Chrome(service=service, options=options) try: driver.get(url) # 显式等待Cookie按钮加载完成并可点击,最长等待10秒 wait = WebDriverWait(driver, 10) consent_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.fc-button.fc-cta-consent.fc-primary-button"))) consent_button.click() # 等待目标元素加载并滚动到视图中央 target_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "champion-recommended-build"))) driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", target_element) # 保存截图 screenshot_path = "uggscreenshot.png" driver.save_screenshot(screenshot_path) print(f"截图已保存到 {screenshot_path}") finally: driver.quit()
替代方案:直接拦截Cookie弹窗
如果不想处理按钮点击,可通过Chrome配置阻止Cookie弹窗,省去点击步骤:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC url = "https://u.gg/lol/champions/kayle/build" options = Options() options.add_argument("--headless") options.add_argument("--start-maximized") options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") # 配置拦截Cookie的偏好设置 prefs = { "profile.default_content_setting_values.cookies": 2, # 2表示阻止所有Cookie "profile.block_third_party_cookies": True } options.add_experimental_option("prefs", prefs) service = Service("chromedriver.exe") driver = webdriver.Chrome(service=service, options=options) try: driver.get(url) # 等待目标元素加载并滚动 target_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "champion-recommended-build"))) driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", target_element) screenshot_path = "uggscreenshot.png" driver.save_screenshot(screenshot_path) print(f"截图已保存到 {screenshot_path}") finally: driver.quit()
补充说明
- 无头模式下建议添加User-Agent,避免网站识别为自动化工具而限制访问。
- 显式等待比固定
time.sleep更可靠,能根据元素实际加载状态执行操作,减少不必要的等待时间。
内容的提问来源于stack exchange,提问作者FireFly
相关产品推荐
相关产品推荐

