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();
source share