Java+Selenium3方法篇28-Actions-鼠标悬停

来源:互联网 发布:怎样变更淘宝店的地区 编辑:程序博客网 时间:2024/06/05 15:43

       有些事件,Selenium没有直接提供方法去操作,像鼠标悬停,一般测试场景鼠标悬停分两种常见,一种是鼠标悬停在某一个元素上方,然后会出现下拉子菜单,第二种就是在搜索输入过程,选择自动补全的字段。关于鼠标悬停,selenium把这个方法放在了Actions.java文件中,先来看看鼠标悬停出现下拉菜单的情况。

package lessons;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.interactions.Actions;public class ElementOpration {public static void main(String[] args) throws Exception {                  System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");                     WebDriver driver = new ChromeDriver();               driver.manage().window().maximize();                 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);                  driver.get("https://www.baidu.com/");                 Thread.sleep(2000);                // 设置        WebElement settings = driver.findElement(By.xpath("//*[@id='u1']/a[8]"));                Actions action = new Actions(driver);        action.moveToElement(settings).perform();                driver.findElement(By.linkText("高级搜索")).click();           }  }
再来看看搜索输入,选择自动补全的字段如何自动化实现。

package lessons;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.interactions.Actions;public class ElementOpration {public static void main(String[] args) throws Exception {                  System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");                     WebDriver driver = new ChromeDriver();               driver.manage().window().maximize();                 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);                  driver.get("https://www.baidu.com/");                 Thread.sleep(1000);                // 设置        WebElement inputbox = driver.findElement(By.id("kw"));        inputbox.sendKeys("selenium a");                // 自动补全其中一个选择项        WebElement auto_text = driver.findElement(By.xpath("//*[@id='form']/div/ul/li[@data-key='selenium api文档']"));                Actions action = new Actions(driver);        action.moveToElement(auto_text).click().perform();}}
Actions类中鼠标悬停方法就介绍到这里。


原创粉丝点击