selenium常用的鼠标操作

selenium常用的出表操作

selenium常用的鼠标操作

基本代码演示

package com.test.demo;

import com.test.Init_Page;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.testng.annotations.Test;

/**
 * @author Huangtian
 * @create 2019-03-11 14:39
 */
public class Action_Mouse extends Init_Page {
    @FindBy(id = "menu-head1")
    WebElement menu_head1;

    @Test
    public void action_mouse() {
        driver.get("https://www.ezcun.com/");
        super.setWindowMax();//父类创建了浏览器最大化的方法
        /**
         * 模拟鼠标操作 Actions 类
         */
        Actions action = new Actions(driver);
        //鼠标悬停
        action.moveToElement(menu_head1).perform();
        /**
         *  鼠标左击
         *  action.click().perform();
         *  鼠标右击
         *  action.contextClick().perform();
         *  鼠标双击
         *  action.doubleClick().perform();
         */
    }
}

tips:

当鼠标操作没有执行时,常见的原因除了是元素没有定位到,还有就是在少了perform()方法导致鼠标事件没有执行;