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

Selenium中禁用弹窗通知代码无效问题求助

问题分析与解决方法

你用的--disable-popup-blocking参数是用来禁用浏览器原生的弹窗拦截功能,但图里的弹窗是网站自身通过JavaScript实现的模态对话框(Modal),不属于浏览器级别的弹窗,所以这个参数完全不起作用。

下面给你几种可行的解决方式:

方法一:定位关闭按钮直接点击

先找到弹窗的关闭元素,用Selenium模拟点击操作:

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()
driver = webdriver.Chrome(options=chrome_options)
url="https://www.fluidmeet.com/search/Dubai/office-spaces"
driver.get(url)

# 等待弹窗出现并点击关闭按钮(根据元素实际定位调整)
try:
    close_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "button.close-modal"))
    )
    close_btn.click()
except Exception as e:
    print("弹窗未出现或关闭失败:", e)

注意:需要根据网页实际的关闭按钮选择器调整,比如用XPath、ID等,你可以通过浏览器开发者工具查看该元素的定位信息。

方法二:执行JavaScript隐藏弹窗

如果找不到关闭按钮,直接用JS移除或隐藏弹窗元素:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
driver = webdriver.Chrome(options=chrome_options)
url="https://www.fluidmeet.com/search/Dubai/office-spaces"
driver.get(url)

# 执行JS隐藏弹窗(替换为实际的弹窗元素选择器)
driver.execute_script("document.querySelector('.modal-container').style.display = 'none';")

方法三:设置Cookie跳过弹窗

有些网站会用Cookie记录用户是否关闭过弹窗,你可以手动添加对应的Cookie:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
driver = webdriver.Chrome(options=chrome_options)
url="https://www.fluidmeet.com/search/Dubai/office-spaces"

# 先访问网站域名,再添加Cookie
driver.get("https://www.fluidmeet.com")
driver.add_cookie({"name": "modal_closed", "value": "true", "domain": ".fluidmeet.com"})
driver.get(url)

需要通过浏览器开发者工具查看网站用来控制弹窗的Cookie名称和值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 14:12:07