You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium无法点击Loritta网站按钮的技术求助

问题分析与解决

核心问题

你切换到reCAPTCHA的iframe完成验证后,没有切回主页面的上下文,此时Selenium的搜索范围还停留在reCAPTCHA的iframe里,自然找不到主页面的「Pegar Premio」按钮。

修改后的代码

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

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_argument(r"--user-data-dir=PathToMyProfile")  # 替换成你的Chrome用户数据实际路径

driver = webdriver.Chrome(options=options)
driver.get("https://loritta.website/br/daily")

# 处理reCAPTCHA验证
# 切换到reCAPTCHA的iframe
WebDriverWait(driver, 10).until(
    EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']"))
)
# 点击reCAPTCHA复选框
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "recaptcha-anchor"))
).click()

# 关键操作:切回主页面上下文
driver.switch_to.default_content()

# 等待「Pegar Premio」按钮可点击后再执行点击
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="daily-wrapper"]/div[3]/div'))
).click()

# 可选:如果需要等待奖励获取的反馈,可添加等待逻辑后再关闭
# WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "替换为奖励成功提示的元素选择器")))
driver.quit()

额外注意事项

  • reCAPTCHA自动化有局限:如果触发图片验证或其他高级验证,纯Selenium代码无法自动完成,可能需要手动介入,不要尝试违规绕过验证。
  • 确认元素定位有效性:如果网站更新页面结构,XPATH可能失效,建议用浏览器开发者工具重新定位「Pegar Premio」按钮。
  • 确保用户数据路径正确:使用已登录Loritta的Chrome用户档案,避免重复登录流程。

内容的提问来源于stack exchange,提问作者Andersonbap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 14:05:29