Python 如何在Selenium中移动鼠标?

Python 如何在Selenium中移动鼠标?,python,selenium-webdriver,phantomjs,mousemove,Python,Selenium Webdriver,Phantomjs,Mousemove,我试图模拟鼠标在随机曲线或抛物线上的移动,这样看起来鼠标实际上在页面上移动。使用Selenium,我只知道如何单击元素,但在某些网站上,这并不能模拟真实用户。我希望鼠标沿着我计算的随机线移动,然后单击元素。文档中说您可以使用函数。使用Selenium Webdriver,您可以使用“操作”来完成此操作。假设webDriver是selenium驱动程序的实例,下面是一段Java代码: Actions action = new Actions(webDriver); // Firs

我试图模拟鼠标在随机曲线或抛物线上的移动,这样看起来鼠标实际上在页面上移动。使用Selenium,我只知道如何单击元素,但在某些网站上,这并不能模拟真实用户。我希望鼠标沿着我计算的随机线移动,然后单击元素。

文档中说您可以使用函数。

使用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();
您可以参考此url了解所有可能的操作(双击、按住…)

事实上,在我的测试之后,webdriver不可能在网站上模拟真实用户的操作。这是因为鼠标不会执行“可见”移动:(。即使你传递一个代码,然后让动作通过每个像素,它也不会工作


像这样的代码(以下代码中可能有错误)不会很好地工作。我只是尝试了一下,没有看到任何可见的鼠标移动。顺便说一句,测试后我发现,一旦您将参数传递给“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();
WebElement元素=新的WebDriverWait(驱动程序,10)。直到(ec);
//获取元素的位置
Point=element.getLocation();
int x=点x;
int y=点y;
//将鼠标放在花药元件上
WebElement element1=driver.findElement(By.xpath(“//a[@cid='link25118']”);
point1=element1.getLocation();
int x1=点1.x;
int y1=点1.y;
动作。移动到元素(元素1);
action.perform();
//计算偏移量
int offsetX=x1-x>0?x1-x:x-x1;
int offsetY=y1-y>0?y1-y:y-y1;
//使用“按偏移移动”模拟沿图元移动,然后单击
int offset=offsetX>offsetY?offsetX:offsetY;
对于(int i=0;ioffsetY?offsetY:offsetX)){
if(offsetX>offsetY){
action.moveByOffset((offsetX-offsetY)*(x1>x?1:-1),0.perform();
}否则{
action.moveByOffset(0,(offsetY-offsetX)*(y1>y?1:-1)).perform();
}
打破
}
如果((x1>x)和&(y1>y)){
//直截了当
action.moveByOffset(1,1.perform();
}如果((x1>x)和&(y1y)){
//左下
action.moveByOffset(-1,1.perform();
}
}
动作。单击();

Python代码如下所示(假设您的浏览器是Firefox):

请注意,这不会移动您的物理光标,而只移动硒元素的不可见光标。若要查看它是否有效,元素必须具有某种“悬停”效果。 此外,如果已将光标移动到某个图元,并希望相对地重新定位该图元,则可以使用:

action.move_by_offset(10, 20)    # 10px to the right, 20px to bottom
action.perform()
甚至更短:

action.move_by_offset(10, 20).perform()
更多文档如下:

使用Selenium单击元素时,您不需要模拟什么?“鼠标移动”这不是python,但我了解它的要点。感谢python,它完全相同,请阅读
action.move_by_offset(10, 20).perform()