I'm trying to simulate mouse movement across a random curve line or parabola so it looks like the mouse actually moved across the page. With Selenium, I only know how to click on an element but that doesn't simulate a real user on some websites. I want the mouse to move along a random line that I calculate, then click the element.
The Python code would look like this (assuming your browser is Firefox):
driver = webdriver.Firefox(executable_path=driver_path)
action = webdriver.ActionChains(driver)
element = driver.find_element_by_id('your-id') # or your another selector here
action.move_to_element(element)
action.perform()
Please note that this doesn't move your physical cursor, but only invisible cursor of Selenium. To see if it worked, element must have some 'hover' effect. Also if you have already moved your cursor to an element and want to re-position it relatively, you can use:
action.move_by_offset(10, 20) # 10px to the right, 20px to bottom
action.perform()
or even shorter:
action.move_by_offset(10, 20).perform()
More documentation is here: https://selenium-python.readthedocs.io/api.html
The docs say you can use move_by_offset(xoffset, yoffset)
function.
Acually it's impossible to simulate a real user's operation on a website by webdriver after mine testing. It is because the mouse won't perform the 'visible' movement :( . Even you pass a code then let the action go through every pixels, it dosen't work.
A code like this (maybe faults in following code) won't work well. I just had a try and didn't see any visible mouse movement. BTW, after testing I've found that once you passed parameters to 'moveByOffset' then the x and y coordinates would begin with 'left-top' point. Maybe there is no use to move to another element firstly.
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();
© 2022 - 2024 — McMap. All rights reserved.