Selenium WebDriver / Java - Simulate Human Like Cursor Movement

泪湿孤枕 提交于 2020-11-25 03:55:29

问题


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 :)


回答1:


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


来源:https://stackoverflow.com/questions/64456577/selenium-webdriver-java-simulate-human-like-cursor-movement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!