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

Selenium/Python无法通过window_handle检测Scopus弹出窗口的处理问题

Scopus爬取时弹窗无法关闭的问题求助

翻出学习编程阶段编写的Scopus数据爬取旧代码,目前Scopus网站会弹出一个无法通过window_handle检测到的窗口。尝试用driver.find_element定位关闭按钮(对应代码最后一行)但无法生效,试过window_handle检测弹窗也没用,寻求解决方案。

相关代码如下:

import openpyxl
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import time
import pandas as pd
from openpyxl import load_workbook

DOI = []
TITLE = []
NUM_AUTHORS = []
NUM_AFFILIATIONS = []
AUTHORS = []
YEAR = []
JOURNAL = []
DOCUMENT_TYPE = []
COUNTRIES = []
SEARCH = []
DATES = []

chrome_driver_path = "C:\\Development\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)

driver.get("https://www.scopus.com/search/form.uri?display=basic#basic")
# searching details
search = input("Search documents: \\n")
SEARCH.append(search)
date = input("Do you want to specify dates?(Yes/No)")
if date.capitalize() == "Yes":
    driver.find_element(By.CLASS_NAME, 'flex-grow-1').send_keys(search)
    driver.find_element(By.XPATH,
                        "/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div["
                        "2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel[1]/div/form/div[2]/div[1]/button["
                        "2]/span[2]").click()
    time.sleep(1)
    starting_date = input("Put starting year.")
    to_date = input("Put end date.")
    DATES.append(starting_date)
    DATES.append(to_date)
    drop_menu_from = Select(driver.find_element(By.XPATH,
                                                "/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div["
                                                "2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel["
                                                "1]/div/form/div[2]/div[1]/els-select/div/label/select"))
    drop_menu_from.select_by_visible_text(starting_date)
    drop_menu_to = Select(driver.find_element(By.XPATH,
                                              "/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div["
                                              "2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel["
                                              "1]/div/form/div[2]/div[2]/els-select/div/label/select"))
    drop_menu_to.select_by_visible_text(to_date)
    driver.find_element(By.XPATH,
                        '/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div['
                        '2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel[1]/div/form/div[4]/div['
                        '2]/button/span[1]').click()
else:
    DATES = ["XXX", "YYY"]
    driver.find_element(By.CLASS_NAME, 'flex-grow-1').send_keys(search)
    driver.find_element(By.XPATH,
                        "/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div["
                        "2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel[1]/div/form/div[2]/div["
                        "2]/button").click()
time.sleep(2)
doc_num = int(driver.find_element(By.XPATH,
                                  "/html/body/div[1]/div/div[1]/div/div/div[3]/form/div[1]/div/header/h1/span[1]").text.replace(
    ",", ""))

time.sleep(5)

driver.find_element((By.XPATH, "/html/body/div[11]/div[2]/div[1]/div[4]/div/div[2]/button")).click()

解决方案建议

  • 更换元素定位方式:你用的是绝对XPATH,Scopus页面结构更新后会直接失效。改用相对定位,比如通过按钮的文本内容或class属性定位,示例:
    # 按文本定位(假设按钮文本是"Close")
    driver.find_element(By.XPATH, "//button[contains(text(), 'Close')]").click()
    # 或按class组合定位
    driver.find_element(By.CSS_SELECTOR, "button.close-modal-btn").click()
    
  • 使用显式等待替代固定休眠:time.sleep(5)无法保证弹窗加载完成,改用Selenium的显式等待确保元素可点击后再操作:
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    close_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Close')]")))
    close_button.click()
    
  • 检查是否存在iframe嵌套:如果弹窗是嵌入在iframe内的,需要先切换到iframe才能定位元素:
    # 等待iframe加载并切换
    iframe = wait.until(EC.presence_of_element_located((By.TAG_NAME, "iframe")))
    driver.switch_to.frame(iframe)
    # 定位并点击关闭按钮
    close_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Close')]")))
    close_button.click()
    # 操作完成切回主文档
    driver.switch_to.default_content()
    
  • 尝试JavaScript强制点击:如果元素存在但因遮挡或交互限制无法点击,用JS执行点击操作:
    close_button = wait.until(EC.presence_of_element_located((By.XPATH, "你的定位路径")))
    driver.execute_script("arguments[0].click();", close_button)
    

内容的提问来源于stack exchange,提问作者Łukasz Zedler

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 21:00:58