V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Features
V2EX  ›  问与答

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

  •  
  •   Features · Jul 23, 2023 · 1403 views
    This topic created in 1008 days ago, the information mentioned may be changed or developed.
    webpack 构建的网页
    发现用 querySelector 获取的元素,确实获取到了,能打印出正确的属性
    但是使用 click()方法进行模拟点击无效

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

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

    ['mousedown', 'click', 'mouseup'].forEach(type => { element.dispatchEvent(new MouseEvent(type)) })
    Features
        3
    Features  
    OP
       Jul 23, 2023
    @des 之前试过,没有效果
    Rache1
        4
    Rache1  
       Jul 24, 2023
    你得先确认一下,他这个是监听的哪个事件 🧐,万一别人监听的不是 click 呢
    Features
        5
    Features  
    OP
       Jul 24, 2023
    @Rache1 是啊,试了 tab,keyup ,keydown,keypress 都不行。。。
    Rache1
        6
    Rache1  
       Jul 24, 2023
    @Features 选中元素,在控制台执行:getEventListeners($0) ,看看绑定了哪些事件
    Features
        7
    Features  
    OP
       Jul 24, 2023
    @Rache1 谢谢,我找到了,他这个太离谱了,要先触发 touchstart ,再触发 touchend ,再触发 click 才行。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5905 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 03:34 · PVG 11:34 · LAX 20:34 · JFK 23:34
    ♥ Do have faith in what you're doing.