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

Selenium+Python:如何点击Google Pay弹出iframe结账窗口中的Pay按钮?

问题:Selenium切换gPay iframe并点击Pay按钮失败

我能进入结账页面并点击gPay按钮,但点击后弹出的iframe里的Pay按钮无法用Selenium点击。相关信息如下:

iframe的HTML代码

<iframe style="border: 0px none; vertical-align: initial; display: block; width: 100%; position: static; top: auto; visibility: visible; z-index: auto; background-color: inherit; height: 510px; left: auto; min-height: auto; opacity: 1; transition: all 0s ease 0s;" src="https://payments.google.com/payments/u/0/embedded/buy_flow?tc=98%2C97%2C81%2C83%2xxxxxxxxxxxx" id="sM432dIframe" name="sM432dIframe" data-widget="current" title="" frameborder="0"></iframe>

Pay按钮的HTML代码

<div role="button" class="goog-inline-block jfk-button jfk-button-action b3-button b3id-button b3-ripple-container b3-primary-button" tabindex="0" style="user-select: none;" data-start-event-id="-131" data-was-visible="true">Pay<div class="b3id-ripple b3-ripple" aria-hidden="true" data-was-visible="true"></div></div>

尝试过的无效代码

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "sM432dIframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "goog-inline-block jfk-button jfk-button-action b3-button b3id-button b3-ripple-container b3-primary-button"))).click()
time.sleep(10)
driver.switch_to.frame("sM432dIframe")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "goog-inline-block jfk-button jfk-button-action b3-button b3id-button b3-ripple-container b3-primary-button"))).click()

解决方案

1. 问题根源

  • By.CLASS_NAME仅支持匹配单个类名,传入多个类名会直接导致定位失败
  • 需确保iframe的定位符准确,原代码里的**sM432dIframe**是标注符号,实际元素id为sM432dIframe

2. 修正后的代码

方式一:用CSS选择器匹配多类名定位按钮

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

# 等待iframe加载完成并切换上下文
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "sM432dIframe")))

# 用CSS选择器匹配多类名,等待按钮可点击后触发点击
pay_button = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable(
        (By.CSS_SELECTOR, "div.goog-inline-block.jfk-button.jfk-button-action.b3-button.b3id-button.b3-ripple-container.b3-primary-button")
    )
)
pay_button.click()

# 操作完成后切换回主页面上下文(若后续需操作其他元素)
driver.switch_to.default_content()

方式二:用按钮文本定位(更稳定)

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

# 切换到iframe上下文
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "sM432dIframe")))

# 通过按钮文本+角色属性定位
pay_button = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable(
        (By.XPATH, "//div[text()='Pay' and @role='button']")
    )
)
pay_button.click()

# 切回主页面上下文
driver.switch_to.default_content()

3. 额外优化建议

  • 若iframe的id/name是动态生成的,可通过src属性包含特征字符串定位:
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src*='payments.google.com']")))
    
  • 避免使用time.sleep()硬等待,优先用WebDriverWait显式等待,提升脚本稳定性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 04:55:18