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

使用Selenium启动Chrome时无法定位谷歌弹窗广告iframe问题

Selenium启动Chrome后谷歌弹窗广告iframe缺失问题排查与解决

问题现象

使用日常浏览用的普通Chrome浏览器访问站点时,可以正常定位到谷歌弹窗广告对应的iframe及其内部的#document,也能找到关闭广告的所有按钮。但通过Selenium启动Chrome浏览器后,该iframe不存在,即使手动检查元素,也无法在DOM中找到该弹窗广告的对应元素。

现象截图

  • 广告正常展示时的iframe及内部DOM结构:
    广告可见时的iframe结构
  • Selenium启动Chrome时的DOM结构:
    仅存在名称相似但对应页面非弹窗广告的元素,弹窗广告未在DOM中加载
    Selenium启动Chrome时的DOM结构

排查思路与解决方案

  • 原因1:Chrome自动化启动时默认开启广告、弹窗拦截规则
    Selenium启动的Chrome默认加载的安全策略会自动拦截第三方广告类iframe和弹窗,可通过启动参数关闭对应拦截能力,配置示例如下:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 关闭弹窗拦截
chrome_options.add_argument("--disable-popup-blocking")
# 关闭内置广告拦截
chrome_options.add_argument("--disable-ads")
# 关闭安全浏览防护
chrome_options.add_argument("--disable-web-security")
# 关闭隐私沙盒、跟踪保护功能
chrome_options.add_argument("--disable-features=PrivacySandboxSettings4,TrackingProtection")

driver = webdriver.Chrome(options=chrome_options)
  • 原因2:Selenium默认加载空白用户配置,站点未推送广告
    普通Chrome使用的是带有浏览历史、Cookie、用户偏好的本地配置文件,Selenium默认启动的是全新的无痕模式配置,站点识别到新用户不会立刻推送弹窗广告。可配置Selenium加载本地Chrome的用户配置文件:
# 路径替换为你本地Chrome用户配置的实际路径
# Windows路径示例:C:\\Users\\xxx\\AppData\\Local\\Google\\Chrome\\User Data
# macOS路径示例:/Users/xxx/Library/Application Support/Google/Chrome
chrome_options.add_argument("user-data-dir=替换为你的本地Chrome配置路径")
  • 原因3:站点检测到自动化访问特征,屏蔽广告加载
    大部分站点和广告服务商都会检测navigator.webdriver等Selenium专属的特征值,识别到自动化访问后会直接不加载广告资源。可通过配置隐藏自动化特征,或直接使用undetected-chromedriver替代原生ChromeDriver绕过检测:
# 隐藏自动化特征的补充配置
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("useAutomationExtension", False)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 16:24:04