Python 如何在Selenium中移动鼠标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32167577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move the mouse in Selenium?
提问by User
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.
我正在尝试模拟鼠标在随机曲线或抛物线上的移动,因此看起来鼠标实际上是在页面上移动的。使用 Selenium,我只知道如何点击一个元素,但这并不能模拟某些网站上的真实用户。我希望鼠标沿着我计算的随机线移动,然后单击该元素。
回答by Sait
The docs say you can use move_by_offset(xoffset, yoffset)
function.
文档说你可以使用move_by_offset(xoffset, yoffset)
函数。
回答by Cedric
With Selenium Webdriver, you can use "Actions" to do this. Lets say that webDriver is your instance of selenium driver, here's a piece of code in Java :
使用 Selenium Webdriver,您可以使用“操作”来执行此操作。假设 webDriver 是您的 selenium 驱动程序实例,这是一段 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 refer to this url for all possible actions (double click, hold...) http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html
您可以参考此 url 以获取所有可能的操作(双击、按住...) http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html
回答by J.Lyu
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.
事实上,经过我的测试,webdriver 无法模拟真实用户在网站上的操作。这是因为鼠标不会执行“可见”运动:(。即使你传递了一个代码,然后让动作通过每个像素,它也不起作用。
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.
像这样的代码(可能是以下代码中的错误)将无法正常工作。我刚刚试了一下,没有看到任何可见的鼠标移动。顺便说一句,经过测试,我发现一旦您将参数传递给“moveByOffset”,那么 x 和 y 坐标将从“左上角”点开始。也许先移动到另一个元素没有用。
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();
回答by Eugene Chabanov
The Python code would look like this (assuming your browser is Firefox):
Python 代码如下所示(假设您的浏览器是 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 use don'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:
请注意,使用时不要移动您的物理光标,而只能移动 Selenium 的不可见光标。要查看它是否有效,元素必须具有某种“悬停”效果。此外,如果您已经将光标移动到一个元素并想相对地重新定位它,您可以使用:
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