V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Features
V2EX  ›  问与答

chrome 下的 web 元素模拟点击有什么办法吗?

  •  
  •   Features · 278 天前 · 640 次点击
    这是一个创建于 278 天前的主题,其中的信息可能已经有所发展或是发生改变。
    webpack 构建的网页
    发现用 querySelector 获取的元素,确实获取到了,能打印出正确的属性
    但是使用 click()方法进行模拟点击无效

    自己构造 Event 事件也没有效果
    目前只能依赖按键精灵模拟点击,极度依赖屏幕分辨率

    有什么好的通用的办法可以实现点击吗?
    7 条回复    2023-07-24 12:55:11 +08:00
    asche910
        1
    asche910  
       278 天前
    插眼,等一个回答
    des
        2
    des  
       278 天前
    试试用 dispatchEvent ?

    ['mousedown', 'click', 'mouseup'].forEach(type => { element.dispatchEvent(new MouseEvent(type)) })
    Features
        3
    Features  
    OP
       278 天前
    @des 之前试过,没有效果
    Rache1
        4
    Rache1  
       278 天前
    你得先确认一下,他这个是监听的哪个事件 🧐,万一别人监听的不是 click 呢
    Features
        5
    Features  
    OP
       278 天前
    @Rache1 是啊,试了 tab,keyup ,keydown,keypress 都不行。。。
    Rache1
        6
    Rache1  
       278 天前
    @Features 选中元素,在控制台执行:getEventListeners($0) ,看看绑定了哪些事件
    Features
        7
    Features  
    OP
       278 天前
    @Rache1 谢谢,我找到了,他这个太离谱了,要先触发 touchstart ,再触发 touchend ,再触发 click 才行。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2708 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:44 · PVG 18:44 · LAX 03:44 · JFK 06:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.