Selenium之元素进阶操作--鼠标左击,右击,双击,悬停(Action)的应用

常见的点击方法集锦:

参数:
    1.driver是我们的浏览器
    2.Actions是我们系统内置的执行鼠标一系列操作的对象
鼠标左击:Actions actions=new Actions(driver);
        actions.click(这里传入我们需要点击的控件元素,WebElement类型).perform();
        //例如:
        //找到我们慕课网首页登录的id
        WebElement login=driver.findElement(By.id("js-signin-btn"));
        //创建动作对象,并且指定操作的浏览器
        Actions actions=new Actions(driver);
        //单击操作传入参数,并且使用perform()提交,生效
        actions.click(login).perform();
鼠标右击:Actions actions=new Actions(driver);
        actions.contextClick(这里传入我们需要点击的控件元素,WebElement类型).perform();
//鼠标双击:鼠标双击的效果跟我们实际自己点击两次的效果有些出入,也不是那么智能
    Actions actions=new Actions(driver);
    actions.doubleClick(login).perform();
鼠标悬停:
    Actions actions=new Actions(driver);
    actions.moveToElement(这里传入我们需要点击的控件元素,WebElement类型).perform();

前三种操作比较基础就不多做介绍,主要讲一下第四种悬停操作容易犯得错误

鼠标悬停实战

eg:

è¿éåå¾çæè¿°

整体的操作流程: 鼠标指针悬停在"前端开发"一栏,然后点击该栏目中的"HTML/CSS"即可

html“id”导航

è¿éåå¾çæè¿°

 

代码:

package com.wushuai.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ActionTest {
    public WebDriver driver;
    //初始化chrome浏览器驱动,并且让chrome浏览器打开慕课网首页
    public void initDriver() 
    {
        System.setProperty("webdriver.chrome.driver", "D:\\java\\chromedriver-32\\chromedriver.exe");
        //初始化chrome浏览器
        driver=new ChromeDriver();
        driver.get("http://www.imooc.com/");
        //屏幕最大化
        driver.manage().window().maximize();
    }
    /*
     * 使用Action执行鼠标左击,右击,双击,悬停操作
     * 
     * */
    public void actionTest() {
        //找到我们慕课网首页登录的id
        WebElement login=driver.findElement(By.id("a"));
        //创建动作对象,并且指定操作的浏览器
        Actions actions=new Actions(driver);
        //单击操作传入参数,并且使用perform()提交,生效
        //actions.click(login).perform();
        //双击操作
        //actions.doubleClick(login).perform();
        //右击操作
        //actions.contextClick(login).perform();
        //actions.moveToElement().perform();
        //鼠标悬停操作
        actions.moveToElement(login).perform();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ActionTest at=new ActionTest();
        at.initDriver();
        at.actionTest();
    }

}

报错:

no such element: Unable to locate element: {"method":"id","selector":"a"}
不能定位元素,也就是说定位元素失败,就是说我们找寻的id为“a”的可能不止一种,系统不能准确定位
所以我们可以采用层级定位方法,就是先定位找到父元素,再借助父元素再来找子元素
 

正确代码:

package com.wushuai.test;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ActionTest {
    public WebDriver driver;
    //初始化chrome浏览器驱动,并且让chrome浏览器打开慕课网首页
    public void initDriver() 
    {
        System.setProperty("webdriver.chrome.driver", "D:\\java\\chromedriver-32\\chromedriver.exe");
        //初始化chrome浏览器
        driver=new ChromeDriver();
        driver.get("http://www.imooc.com/");
        //屏幕最大化
        driver.manage().window().maximize();
    }
    /*
     * 使用Action执行鼠标左击,右击,双击,悬停操作
     * 
     * */
    public void actionTest() {
        //找到我们慕课网首页包含前段开发,后端开发,移动开发等着一系列条目的className
        WebElement login=driver.findElement(By.className("menuContent"));
        //List集合接收前段开发,后端开发,移动开发的条目,注意用的是findElements不是findElement,多一个"s"
        List<WebElement> elements=login.findElements(By.className("item"));
        //创建动作对象,并且指定操作的浏览器
        Actions actions=new Actions(driver);
        //单击操作传入参数,并且使用perform()提交,生效
        //actions.click(login).perform();
        //双击操作
        //actions.doubleClick(login).perform();
        //右击操作
        //actions.contextClick(login).perform();
        //actions.moveToElement().perform();
        //鼠标悬停在List集合的第一个元素即"前段开发"
        actions.moveToElement(elements.get(0)).perform();
        //点击"前段开发"中包含的"HTML/CSS"条目
        driver.findElement(By.partialLinkText("HTML/CSS")).click();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ActionTest at=new ActionTest();
        at.initDriver();
        at.actionTest();
    }

}

总结:

1.鼠标双击使用Actions操作与我们实际操作有出入
2.要充分利用父元素与子元素的层级关系,理清关系,防止报错:
no such element: Unable to locate element

 

 

 

 

 

 

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值