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

如何使用Selenium Python将光标移至浏览器工具栏触发弹窗

如何通过Selenium将光标移至浏览器工具栏触发退出意图模态框

要触发目标页面的退出意图模态框,核心是模拟光标移动到浏览器工具栏(页面视口外的顶部区域),以下是用Selenium实现的具体方案:

实现代码

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


class TestExitIntent:

    def setup_method(self):
        # 初始化Chrome驱动,打开目标页面并设置隐式等待
        self.driver = webdriver.Chrome()
        self.driver.get('https://the-internet.herokuapp.com/exit_intent')
        self.driver.implicitly_wait(5)

    def test_exit_intent(self):
        # 创建ActionChains对象用于模拟鼠标操作
        action = ActionChains(self.driver)
        # 将光标移动到页面视口外的位置(模拟移向浏览器工具栏)
        # 注:若当前参数未触发模态框,可尝试调整为(0, -10)这类纵向偏移至视口外的参数
        action.move_by_offset(150, 0)
        action.perform()

        # 点击模态框的关闭按钮
        self.driver.find_element(By.XPATH, "//div[@class='modal-footer']/p").click()

关键说明

  • 借助ActionChains类模拟鼠标移动,move_by_offset方法通过设置坐标偏移量,将光标移至页面视口之外(浏览器工具栏所在区域),以此触发退出意图模态框。
  • 若初始偏移量未触发模态框,可根据浏览器窗口尺寸调整参数,通常将y轴设为负数(如(0, -10))更精准,因为浏览器工具栏位于页面顶部的视口外。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 17:50:20