详解Java Selenium中的鼠标控制操作

简介

本文主要讲解如何用java Selenium 控制鼠标在浏览器上的操作方法。主要列举的代码示例,无图显示。可以自己上代码执行操作看效果。

鼠标控制

单击左键

模拟完成单击鼠标左键的操作,一般点击进入子页面等会用到。

第一种通过WebElement对象的click()方法实现单击左键

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class SeleniumDemo {
    private final static String webDriver = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriver, webDriverPath);
        WebDriver driver= new ChromeDriver();
        //Jenkins 登录界面
        driver.get("http://119.167.159.214:8080/login");
        Thread.sleep(2000);
        //定位按钮元素
        WebElement commentPlugin=driver.findElement(By.name("Submit"));
        //执行单击操作
        commentPlugin.click();
    }
}

第二种通过Actions对象的click()方法实现单击左键

        //定位按钮元素
        WebElement commentPlugin=driver.findElement(By.name("Submit"));
        // 实例化Actions类对象:actions,并将driver传给actions
        Actions actions = new Actions(driver);
        //无定位点击
        actions.click().perform();
        //定位web元素后点击
        actions.click(commentPlugin).perform();

.perform()方法是动作执行的意思,每个动作方法必须再使用.perform()才能执行。

单击右键

鼠标右击的操作与左击有很大不同,需要使用 Actions 。

     //定位按钮元素
        WebElement commentPlugin=driver.findElement(By.name("Submit"));
        // 实例化Actions类对象:actions,并将driver传给actions
        Actions actions = new Actions(driver);
        //无定位右键点击
        actions.contextClick().perform();
        //定位web元素后右键点击
        actions.contextClick(commentPlugin).perform();

双击左键

模拟鼠标双击操作。

    //定位按钮元素
        WebElement commentPlugin=driver.findElement(By.name("Submit"));
        // 实例化Actions类对象:actions,并将driver传给actions
        Actions actions = new Actions(driver);
        //无定位双击
        actions.doubleClick().perform();
        //定位web元素后双击
        actions.contextClick(commentPlugin).perform();

按压左键

模拟鼠标按下左键不松手

       //无定位鼠标按压左键
        actions.clickAndHold().perform();
        //定位web元素后鼠标按压左键
        actions.clickAndHold(commentPlugin).perform();

鼠标箭头移动

模拟鼠标箭头移动

//移动到定位的元素位置上    
 actions.moveToElement(commentPlugin).perform();

鼠标释放

模拟鼠标按压后,释放鼠标

        //鼠标释放
        actions.release().perform();
        //定位释放鼠标释放
        actions.release(commentPlugin).perform();

鼠标拖拽

模拟鼠标选中web元素后拖拽到指定位置的操作

       //需要拖拽的web元素
        WebElement source=driver.findElement(By.id("source"));
        //拖拽的目标元素的位置
        WebElement target=driver.findElement(By.id("target"));
        //拖拽操作
        actions.dragAndDrop(source,target).perform();

鼠标等待

一般点击网页的某个按钮,网页需要渲染一端时间才会出现新的dom树,所以我们需要操作等待执行。

        //定位web元素后鼠标按压左键
        actions.clickAndHold(commentPlugin).perform();
        //动作等待3秒
        actions.pause(5000);
        //释放鼠标左键
        actions.release(commentPlugin).perform();

原文地址:https://blog.csdn.net/weixin_40986713/article/details/128546182