How to move the mouse in Selenium?

I'm trying to simulate the movement of a mouse along a random curve or parabola, so it looks like the mouse was actually moving around the page. With Selenium, I only know how to click on an element, but it does not imitate a real user on some sites. I want the mouse to move along the random line that I am calculating, then click on the item.

+4
source share
3 answers

With Selenium Webdriver, you can use "Actions" to do this. Lets say webDriver is your instance of the selenium driver, here is part of the code in Java:

Actions action = new Actions(webDriver); // First, go to your start point or Element action.moveToElement(startElement); action.perform(); // Then, move the mouse action.moveByOffset(x,y); action.perform(); // Then, move again (you can implement your one code to follow your curve...) action.moveByOffset(x2,y2); action.perform(); // Finaly, click action.click(); action.perform(); 

You can link to this URL for all possible actions (double click, hold ...) http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html

+2
source

The docs say you can use move_by_offset(xoffset, yoffset) .

+1
source

It is actually not possible to simulate a real user operation on the webdriver website after testing. This is due to the fact that the mouse will not perform the “visible” movement :( Even you pass the code, then let the action go through each pixel, it does not work.

Such code (possibly errors in the following code) will not work. I just tried and did not see any visible mouse movement. BTW, after testing, I found that after you passed the parameters to "moveByOffset", the x and y coordinates started from the "left-top" point. Maybe it makes no sense to move on to another element first.

 WebElement element = new WebDriverWait(driver, 10).until(ec); //Get the postion of the element Point point = element.getLocation(); int x = point.x; int y = point.y; //Let mouse on anther element WebElement element1 = driver.findElement(By.xpath("//a[@cid='link25118']")); Point point1 = element1.getLocation(); int x1 = point1.x; int y1 = point1.y; action.moveToElement(element1); action.perform(); //Calculate offset int offsetX = x1 - x > 0 ? x1 - x : x- x1; int offsetY = y1 - y > 0 ? y1 - y : y - y1; //Use move by offset to simulate moving along the element, then click int offset = offsetX > offsetY ? offsetX : offsetY; for(int i=0; i< offset; i++) { Thread.sleep(1000); if( i == (offsetX > offsetY ? offsetY : offsetX)) { if(offsetX > offsetY) { action.moveByOffset((offsetX - offsetY) * (x1>x?1:-1), 0).perform(); } else { action.moveByOffset(0, (offsetY - offsetX) * (y1>y?1:-1)).perform(); } break; } if((x1 > x) && (y1 > y)) { //right down action.moveByOffset(1, 1).perform(); } else if ((x1 > x) && (y1 < y)) { //right up action.moveByOffset(1, -1).perform(); } else if((x1 < x) && (y1 < y)) { //left up action.moveByOffset(-1, -1).perform(); } else if ((x1 < x) && (y1 > y)) { //left down action.moveByOffset(-1, 1).perform(); } } action.click(); 
0
source

All Articles