electron子窗口真实模拟鼠标点击

const test = (win) => {
  // 假设 win 是你的 BrowserWindow 实例
  win.webContents.debugger.attach();

  const clickAt = async (x, y) => {
    console.log(3333, x, y);
    await win.webContents.debugger.sendCommand('Input.dispatchMouseEvent', {
      type: 'mousePressed',
      x,
      y,
      button: 'left',
      clickCount: 1,
    });

    await win.webContents.debugger.sendCommand('Input.dispatchMouseEvent', {
      type: 'mouseReleased',
      x,
      y,
      button: 'left',
      clickCount: 1,
    });
  };

  // 获取按钮位置
  win.webContents
    .executeJavaScript(
      `
  (function() {
    const el = [...document.querySelectorAll('button[data-tid="m4b_button"]')]
  .find(el => el.textContent.trim() === 'Send');
    el.addEventListener('click', () => {
      console.log('按钮被点了!');
    });
    console.log(111, el)
    if (el) {
      const rect = el.getBoundingClientRect();
      console.log(2222, rect);
      return ({ x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 });
    } else {
      return null;
    }
  })()
`,
    )
    .then((pos) => {
      console.log(111, pos);
      if (pos) {
        clickAt(Math.floor(pos.x), Math.floor(pos.y));
      } else {
        console.warn('未找到按钮');
      }
    });
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值