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

如何用Selenium操作GoFundMe页面Transcend同意管理器Shadow DOM?

自动化处理GoFundMe页面Transcend隐私同意弹窗

我正尝试自动化操作GoFundMe页面上由Transcend管理的隐私同意弹窗,页面地址:https://www.gofundme.com/f/10yr-old-pitt-baby-who-needs-emergency-surgery。

具体需求:

  • 勾选「不要出售/分享我的信息」复选框
  • 点击「确认」按钮

这些元素位于ID为transcend-consent-manager的div所挂载的Shadow DOM内。

以下是我当前的代码:

try:
    # 等待同意管理器加载完成
    manager = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "transcend-consent-manager"))
    )
    print("找到同意管理器")

    # 等待并尝试用JavaScript交互
    js_code = """
    function waitForShadowRoot(callback, maxAttempts = 10) {
        let attempts = 0;
        const check = () => {
            attempts++;
            const manager = document.getElementById('transcend-consent-manager');
            const root = manager ? manager.shadowRoot : null;
            if (root) {
                callback(root);
                return;
            }
            if (attempts < maxAttempts) {
                setTimeout(check, 1000);
            }
        };
        check();
    }

    return new Promise((resolve) => {
        waitForShadowRoot((root) => {
            console.log('找到Shadow Root');
            const checkbox = root.querySelector('input[type="checkbox"]');
            if (checkbox) {
                checkbox.click();
                console.log('点击复选框');

                setTimeout(() => {
                    const button = root.querySelector('button');
                    if (button) {
                        button.click();
                        console.log('点击按钮');
                        resolve(true);
                    } else {
                        resolve(false);
                    }
                }, 1000);
            } else {
                resolve(false);
            }
        });
    });
    """

    print("执行JavaScript与Shadow DOM交互...")
    result = driver.execute_async_script(js_code)

    if result:
        print("成功点击元素")
        return True
    else:
        print("未能找到或点击元素")
        # 打印调试信息
        debug_js = """
        const manager = document.getElementById('transcend-consent-manager');
        return {
            manager: !!manager,
            shadowRoot: manager ? !!manager.shadowRoot : false,
            innerHTML: manager && manager.shadowRoot ? manager.shadowRoot.innerHTML : '无内容'
        }
        """
        debug_info = driver.execute_script(debug_js)
        print("调试信息:", debug_info)
        return False

except Exception as e:
    print(f"错误: {str(e)}")
    return False

隐私同意弹窗截图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 15:47:08