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

Selenium绕过Google Vignette广告:无法定位关闭按钮求助

解决Google广告弹窗无法关闭的问题

核心原因

你遇到的问题根源是广告嵌套在iframe中,Selenium默认只会操作主页面的DOM结构,必须主动切换到广告所在的iframe,才能定位并点击关闭按钮。

代码修改方案

把你注释掉的广告处理部分替换成以下代码,同时注意等待iframe加载完成,避免元素未找到的错误:

# 等待广告iframe加载并切换进去(根据实际iframe属性调整,这里用常见的定位方式)
# 方式1:通过iframe的ID定位(如果有ID的话)
# ad_iframe = WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "广告iframe的ID")))
# 方式2:通过索引定位(如果页面只有一个广告iframe)
ad_iframe = WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))

# 尝试定位关闭按钮,以下几种选择器按需使用
try:
    # 方式1:通过文本定位关闭按钮
    close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Close')]")))
    close_btn.click()
except:
    try:
        # 方式2:通过CSS选择器定位常见的关闭按钮样式
        close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[aria-label='Close']")))
        close_btn.click()
    except:
        # 方式3:通过类名定位(如果关闭按钮有特定类)
        close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "close-button")))
        close_btn.click()

# 切回主页面DOM
driver.switch_to.default_content()

完整修改后的代码

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

chrome_options = Options()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--window-size=1920x1080")
# chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:/Users/gblac/OneDrive/Desktop/Chromedriver.exe')

url = 'https://free-mp3-download.net/'
driver.get(url)

WebDriverWait(driver, 4)
search = driver.find_element(By.ID,'q')
search.send_keys('testing songs')
search.click()

button = driver.find_element(By.ID,'snd')
button.click()

WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,'container'))).click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID,"results_t")));
results = driver.find_element(By.ID,'results_t').find_elements(By.TAG_NAME,'tr')
results[0].find_element(By.TAG_NAME,'a').click()

# 处理广告部分
try:
    # 等待并切换到广告iframe
    ad_iframe = WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))
    # 尝试点击关闭按钮
    close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Close')]")))
    close_btn.click()
except Exception as e:
    print(f"关闭广告失败: {e}")
finally:
    # 切回主页面,避免后续操作受影响
    driver.switch_to.default_content()

注意事项

  • 广告的iframe属性和关闭按钮的选择器可能会随网站更新而变化,如果当前代码失效,打开浏览器开发者工具(F12),手动定位iframe和关闭按钮的属性,替换对应的定位方式即可。
  • 建议使用try-except包裹广告处理逻辑,避免广告加载失败导致整个爬虫中断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 07:50:28