【问题标题】:How to move the mouse in Selenium?如何在 Selenium 中移动鼠标?
【发布时间】:2015-11-17 00:10:51
【问题描述】:

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

【问题讨论】:

  • 什么是使用 Selenium 单击元素而不是您需要模拟的?
  • “鼠标移动”

标签: python selenium-webdriver phantomjs mousemove


【解决方案1】:

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()

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

action.move_by_offset(10, 20)    # 10px to the right, 20px to bottom
action.perform()

甚至更短:

action.move_by_offset(10, 20).perform()

更多文档在这里: https://selenium-python.readthedocs.io/api.html

【讨论】:

    【解决方案2】:

    实际上,经过我的测试,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();
    

    【讨论】:

      【解决方案3】:

      文档说你可以使用move_by_offset(xoffset, yoffset) 函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多