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('未找到按钮');
}
});
};
electron子窗口真实模拟鼠标点击
于 2025-06-16 18:19:19 首次发布