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

如何关闭Selenium脚本运行中弹出的弹窗?点击报错解决方案

问题描述

使用Selenium脚本处理页面弹窗时,无法关闭弹窗,报错信息如下:

color="accent" class="mat-focus-indicator mat-button mat-button-base mat-accent">... is not clickable at point (684, 847). Other element would receive the click:

(Session info: chrome=124.0.6367.61)

原代码:

# Import necessary modules
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import csv
import time

# Set up Chrome service
service = webdriver.chrome.service.Service(ChromeDriverManager().install())

# Initialize Chrome WebDriver with the service
driver = webdriver.Chrome(service=service)

# Navigate to the webpage
driver.get("https://mmiconnect.in/app/exhibition/catalogue/ep2023")

# Close the initial popup if present
try:
    # Find and click the close button if it's clickable
    close_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "close-button")))
    close_button.click()
except:
    # If close button not found, pass
    pass

# List to store the scraped data
siteData = []

# Iterate over pages
for nav in range(1, 33):
    for i in range(1, 750):
        try:
            # Find and click on the buttons
            listing = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'center')))
            lists = listing.find_elements(By.XPATH, "//div[@class='courses']//button[contains(@class, 'mat-focus-indicator')]")

            # Iterate over the buttons
            for listss in lists:
                try:
                    # Click on the button
                    listss.click()
                    # Wait for the popup to appear
                    time.sleep(2)
                    
                    # Check if the popup is still open
                    if EC.visibility_of_element_located((By.ID, "mat-dialog-3"))(driver):
                        # Wait for the popup to appear
                        popup_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "mat-dialog-3")))
                        
                        # Scrape data from the popup
                        table = popup_element.find_element(By.ID, "customers")
                        rows = table.find_elements(By.TAG_NAME, "tr")
                        table_data = []
                        for row in rows:
                            cells = row.find_elements(By.TAG_NAME, "td")
                            row_data = [cell.text for cell in cells]
                            table_data.append(row_data)

                        # Write table data to CSV
                        with open("mmei.csv", mode='a', newline='') as file:
                            writer = csv.writer(file)
                            writer.writerows(table_data)

                        # Execute JavaScript to click the "Close" button within the popup
                        close_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-label, 'Close dialog')]")))
                        driver.execute_script("arguments[0].click();", close_button)
                    
                except Exception as e:
                    # Print error if any
                    print("Error processing popup:", e)
                    pass
                
        except Exception as e:
            # Print error if any
            print("Error iterating over pages:", e)
            continue

# Quit the driver
driver.quit()
解决方案

针对弹窗关闭失败的问题,可从以下几个方向优化:

1. 修正弹窗定位逻辑,避免固定ID

弹窗ID(如mat-dialog-3)是动态生成的,多次打开弹窗后ID会变化,改用通用类名定位弹窗容器:

# 替换原固定ID的弹窗定位
popup_element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.CLASS_NAME, "mat-dialog-container"))
)

2. 精确定位弹窗内的关闭按钮

结合弹窗容器缩小定位范围,避免匹配到页面其他位置的同类按钮:

close_button = WebDriverWait(popup_element, 10).until(
    EC.element_to_be_clickable((By.XPATH, ".//button[contains(@aria-label, 'Close dialog')]"))
)

3. 处理元素遮挡问题

报错提示按钮被其他元素遮挡,可先将按钮滚动到可视区域,再尝试点击:

# 滚动到按钮位置
driver.execute_script("arguments[0].scrollIntoView(true);", close_button)
# 优先尝试原生点击,失败再用JS点击
try:
    close_button.click()
except:
    driver.execute_script("arguments[0].click();", close_button)

4. 替换固定等待,用显式等待提升稳定性

去掉time.sleep(2),改用等待弹窗内关键元素(如表格)加载完成:

# 等待弹窗内的表格出现,确认弹窗加载完成
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "customers")))

优化后的核心代码片段

for listss in lists:
    try:
        listss.click()
        # 等待弹窗内表格加载完成,替代固定sleep
        WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "customers")))
        
        # 动态定位弹窗容器
        popup_element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "mat-dialog-container")))
        
        # 爬取表格数据(原逻辑不变)
        table = popup_element.find_element(By.ID, "customers")
        rows = table.find_elements(By.TAG_NAME, "tr")
        table_data = []
        for row in rows:
            cells = row.find_elements(By.TAG_NAME, "td")
            row_data = [cell.text for cell in cells]
            table_data.append(row_data)

        # 写入CSV(原逻辑不变)
        with open("mmei.csv", mode='a', newline='') as file:
            writer = csv.writer(file)
            writer.writerows(table_data)

        # 定位并关闭弹窗
        close_button = WebDriverWait(popup_element, 10).until(
            EC.element_to_be_clickable((By.XPATH, ".//button[contains(@aria-label, 'Close dialog')]"))
        )
        driver.execute_script("arguments[0].scrollIntoView(true);", close_button)
        try:
            close_button.click()
        except:
            driver.execute_script("arguments[0].click();", close_button)
            
    except Exception as e:
        print("Error processing popup:", e)
        pass

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 06:49:56