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

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

  1. package lessons;  
  2.   
  3. import java.util.concurrent.TimeUnit;  
  4.   
  5. import org.openqa.selenium.By;  
  6. import org.openqa.selenium.JavascriptExecutor;  
  7. import org.openqa.selenium.WebDriver;  
  8. import org.openqa.selenium.WebElement;  
  9. import org.openqa.selenium.chrome.ChromeDriver;  
  10. import org.openqa.selenium.interactions.Actions;  
  11.   
  12. public class ElementOpration {  
  13.     public static void main(String[] args) throws Exception {    
  14.           
  15.         System.setProperty("webdriver.chrome.driver"".\\Tools\\chromedriver.exe");    
  16.              
  17.         WebDriver driver = new ChromeDriver();    
  18.        
  19.         driver.manage().window().maximize();    
  20.          
  21.         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);  
  22.             
  23.         driver.get("https://www.baidu.com/");    
  24.          
  25.         Thread.sleep(2000);  
  26.           
  27.         // 设置  
  28.         WebElement settings = driver.findElement(By.xpath("//*[@id='u1']/a[8]"));  
  29.           
  30.         Actions action = new Actions(driver);  
  31.         action.moveToElement(settings).perform();  
  32.           
  33.         driver.findElement(By.linkText("高级搜索")).click();  
  34.          
  35.     }    
  36. }  
再来看看搜索输入,选择自动补全的字段如何自动化实现。

[java]  view plain  copy
  1. package lessons;  
  2.   
  3. import java.util.concurrent.TimeUnit;  
  4.   
  5. import org.openqa.selenium.By;  
  6. import org.openqa.selenium.JavascriptExecutor;  
  7. import org.openqa.selenium.WebDriver;  
  8. import org.openqa.selenium.WebElement;  
  9. import org.openqa.selenium.chrome.ChromeDriver;  
  10. import org.openqa.selenium.interactions.Actions;  
  11.   
  12. public class ElementOpration {  
  13.     public static void main(String[] args) throws Exception {    
  14.           
  15.         System.setProperty("webdriver.chrome.driver"".\\Tools\\chromedriver.exe");    
  16.              
  17.         WebDriver driver = new ChromeDriver();    
  18.        
  19.         driver.manage().window().maximize();    
  20.          
  21.         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);  
  22.             
  23.         driver.get("https://www.baidu.com/");    
  24.          
  25.         Thread.sleep(1000);  
  26.           
  27.         // 设置  
  28.         WebElement inputbox = driver.findElement(By.id("kw"));  
  29.         inputbox.sendKeys("selenium a");  
  30.           
  31.         // 自动补全其中一个选择项  
  32.         WebElement auto_text = driver.findElement(By.xpath("//*[@id='form']/div/ul/li[@data-key='selenium api文档']"));  
  33.           
  34.         Actions action = new Actions(driver);  
  35.         action.moveToElement(auto_text).click().perform();  
  36.     }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值