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

Selenium Python如何选中搜索框检索内容并选择首个结果解决点击拦截异常

问题根因

报错为元素点击被拦截,核心原因是页面底部的Cookie同意弹窗遮挡了目标下拉选择控件,导致点击事件被弹窗元素拦截无法触发。同时原有CSS选择器未添加id标识前缀#,也会导致元素匹配失败。

修复方案

核心调整点

  • 优先处理Cookie弹窗:页面加载后先关闭Cookie同意弹窗,消除元素遮挡
  • 修正元素选择器:补全id选择器前缀#,保证元素匹配正确
  • 补充跳转后信息提取逻辑:实现URL和ROM信息的获取
  • 增加资源释放逻辑:避免浏览器进程残留占用资源

完整可运行代码

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import asyncio
from selenium.common.exceptions import NoSuchElementException

GOOGLE_CHROME_BIN = '你的chrome二进制文件路径'
CHROME_DRIVER = '你的chromedriver文件路径'

async def realme_rom_search(query):
    url = "https://realmeupdater.com/"
    chrome_options = Options()
    chrome_options.add_argument("--headless=new") # 适配新版Chrome无头模式,兼容性更好
    chrome_options.binary_location = GOOGLE_CHROME_BIN
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-gpu")
    driver = webdriver.Chrome(executable_path=CHROME_DRIVER, options=chrome_options)
    wait = WebDriverWait(driver, 20)
    result = {}
    try:
        driver.get(url)
        # 处理Cookie同意弹窗
        try:
            cookie_agree_btn = wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "button.cc-btn.cc-allow")))
            cookie_agree_btn.click()
            wait.until(expected_conditions.invisibility_of_element_located((By.CLASS_NAME, "cc-window")))
        except TimeoutException:
            # 未出现Cookie弹窗则跳过该步骤
            pass
        
        # 点击设备选择下拉框,补全id选择器前缀#
        select_ccs = "#select2-device-container"
        wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, select_ccs))).click()
        # 定位搜索输入框
        elem = wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "body > span > span > span.select2-search.select2-search--dropdown > input")))
        elem.send_keys(query)
        await asyncio.sleep(1) # 等待搜索结果加载完成
        elem.send_keys(Keys.DOWN)
        elem.send_keys(Keys.ENTER)
        # 等待页面跳转完成
        wait.until(expected_conditions.url_changes(url))
        # 提取目标页面URL
        result['page_url'] = driver.current_url
        # 提取ROM基础信息,可按需扩展更多字段
        try:
            rom_version = wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, ".build-version"))).text
            result['rom_version'] = rom_version
            build_date = driver.find_element(By.CSS_SELECTOR, ".build-date").text
            result['build_date'] = build_date
        except NoSuchElementException:
            result['rom_info'] = "未匹配到对应ROM信息"
    except Exception as e:
        result['error'] = str(e)
    finally:
        # 强制关闭浏览器进程,避免资源泄漏
        driver.quit()
    return result

使用说明

调用函数传入设备型号关键词即可,返回结果字典中会包含目标页面URL、ROM版本、编译时间等信息,若执行出错会返回对应的错误信息。

内容的提问来源于stack exchange,提问作者The Unknown Bengali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 09:15:07