[英]How to get a mouse position in Puppeteer-sharp?
Help get the current mouse position in puppeteer-sharp, I tried adapting Node.JS code for puppeteer sharp, but I can't do it.帮助在 puppeteer-sharp 中获取当前鼠标 position,我尝试为 puppeteer-sharp 调整 Node.JS 代码,但我做不到。
page.evaluate(() => {
document.onmousemove = function(e){
mouseX = e.offsetX;
mouseY = e.offsetY;
console.log(mouseX, mouseY);
}
})
Tried this, but I always get an x coordinate of zero:试过这个,但我总是得到一个 x 坐标为零:
var xOffset = await page.EvaluateExpressionAsync<int>("window.offsetX");
MessageBox.Show(xOffset.ToString());
First, the evaluate function won't get what you write in the console.log
.首先,评估 function 不会得到你在console.log
中写的内容。 You need to return something.你需要返回一些东西。 As you are waiting for an event you can return a promise and resolve that promise inside the event.在等待事件时,您可以返回 promise 并在事件中解析 promise。
page.EvaluateFunctionAsync<Point>(@"() => {
return new Promise(resolve => {
document.onmousemove = function(e){
mouseX = e.offsetX;
mouseY = e.offsetY;
resolve({x: mouseX, y: mouseY});
};
});
}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.