简体   繁体   English

如何在 Puppeteer-sharp 中获得鼠标 position?

[英]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.

相关问题 puppeteer-sharp 如何等待请求 - puppeteer-sharp how to wait requests 如何让 Puppeteer-Sharp 在运行 Docker (.NET Core 6) 的 AWS Elastic Beanstalk 上工作? - How to get Puppeteer-Sharp working on an AWS Elastic Beanstalk running Docker (.NET Core 6)? 木偶操纵者锋利的 NavigationException Firefox - puppeteer-sharp NavigationException Firefox 如何从puppeteer-sharp中获取可读的浏览器/页面错误? - How do I get readable browser/page errors out of puppeteer-sharp? Puppeteer-sharp 加载页面时如何获取所有网络请求和完整响应数据? - How can I get all network requests and full response data when loading a page by Puppeteer-sharp? 如何使用 puppeteer-sharp 和 C# 中的 XPath 获取列表中所有 li 元素的内部文本 - How to get innertext of all li elements in an list with puppeteer-sharp and XPath in C# 如何使用 Puppeteer-Sharp 在 iframe 中填写表格 - How to fill a form inside an iframe with Puppeteer-Sharp 如何使用 puppeteer-sharp touchStart 和 touchEnd 和 touch move - how to use puppeteer-sharp touchStart and touchEnd and touch move 使用 Puppeteer-sharp 启动 Tor 浏览器 - Launch Tor Browser with Puppeteer-sharp <span>使用 puppeteer-sharp 将</span>文本抓取到一个<span>类中</span> - Scraping text into a <span> class with puppeteer-sharp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM