Selenium WebDriver / Java – 模拟类人的光标移动

huangapple go评论100阅读模式
英文:

Selenium WebDriver / Java - Simulate Human Like Cursor Movement

问题

我想自动化Facebook广告管理器中的一个简单任务。这个任务涉及设置一个广告系列并上传一些广告。一个人可能需要30分钟来完成这个任务。然而,他们每次都在做同样的事情。经常还会出错。这是应该自动化的事情。可以在没有人类情感或错误的情况下完成。

Facebook非常敏感,我不希望因为错误的原因被封禁。所以我需要让操作看起来像是人在操作。我可以在点击之间花费时间。然而,光标移动本身需要看起来像是人在操作。出于道德目的,我只需要模拟一个真实的人类点击

假设我有一个我想要将光标移动到的元素:

WebDriver driver;    
    
// 设置Chrome驱动程序的文件路径
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    
// 创建对象
ChromeDriver driver = new ChromeDriver(options);
    
// 前往URL
driver.get("FACEBOOK URL");
    
// 获取元素
driver.findElement(By.xpath("//span[contains(text(), 'Setup Campaign')]"));

作为一个真实的人类,最好的方法是如何将光标移向这个元素呢?

  • 真实的人类会先移动鼠标。不仅仅是点击元素。

  • 他们会缓慢地移动鼠标/光标。可能需要500-1000毫秒。绝对不是瞬间完成。

  • 他们会以曲线方式移动鼠标/光标。不只是一条100%直线。可能会随机移动?因此可能需要一些随机性的元素?

我对Selenium还相对新手,所以任何意见都将不胜感激。

我正在使用Java编写我的代码 Selenium WebDriver / Java – 模拟类人的光标移动

英文:

I want to automate a simple task inside Facebook Ads Manager. This task involves setting up a campaign and uploading some ads. It can take a human 30 minutes to do this. However, they're doing the same thing every single time. Often with mistakes. It's something that should be automated. Done without human emotion or mistakes.

Facebook is very sensitive and I don't want it to ban me for the wrong reasons. So I need to feel human. I can take my time between clicks. However, the cursor movement itself needs to feel human. I only need to simulate a real human click for ethical purposes.

Say I get an element I want to move my cursor towards:

WebDriver driver;    

// Set file path of chrome driver
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

// Create object
ChromeDriver driver = new ChromeDriver(options);

// Go to URL
driver.get("FACEBOOK URL");

// Get element
driver.findElement(By.xpath("//span[contains(text(), 'Setup Campaign')]");

What is the best way to move my cursor towards this element as a real human would?

  • A real human would first move the mouse. Not just click the element

  • They would move the mouse/cursor slowly. It could take up to say 500-1000 milliseconds. Certainly not instantly.

  • They would move the mouse/cursor in a curved fashion. Not just in a 100% straight line. Possibly, in a random fashion? So some elements of randomness may be needed?

I'm quite new to Selenium, so any input would be greatly appreciated.

I am writing my code in Java Selenium WebDriver / Java – 模拟类人的光标移动

答案1

得分: 1

WebDriver不使用操作系统输入;它通过HTTP协议直接与浏览器通信。如果您想要模拟类似于“真实”鼠标输入的通信,您必须使用使用基于操作系统的框架的自动化解决方案。在Windows的情况下,您可以使用如下:

  1. https://github.com/FlaUI/FlaUI(阅读https://github.com/FlaUI/FlaUI/wiki/FAQ以了解如何配置Chrome以公开FlaUI的网络控件的知识)
  2. https://github.com/microsoft/WinAppDriver
英文:

WebDriver doesn't use an operating system input; it communicates directly with the browser via http protocol. If you want to simulate communication like a 'real' mouse input you have to use an automation solution that uses operating system based frameworks. In case of Windows you can use e.g.:

  1. https://github.com/FlaUI/FlaUI (read https://github.com/FlaUI/FlaUI/wiki/FAQ to get the knowledge how to configure Chrome to expose web controls for FlaUI)
  2. https://github.com/microsoft/WinAppDriver

答案2

得分: 0

我理解这并不完全是您所询问的内容,但在这种情况下,我建议您使用 Facebook API 而不是 selenium。

它比您的方法更稳定,并且没有被封禁的风险。

https://developers.facebook.com/docs/marketing-api/reference/v12.0
英文:

I understand that this is not exactly what you asked, but in this case I recommend you to use the Facebook API instead of selenium.

It's more stable than your approach and without the risk of getting banned.

https://developers.facebook.com/docs/marketing-api/reference/v12.0

huangapple
  • 本文由 发表于 2020年10月21日 12:13:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64456577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定