webdriver提供Actions来模拟鼠标悬浮、拖拽和键盘输入等操作,详细代码见org.openqa.selenium.interactions.Actions.本文通过几个实例来说明Actions的相关操作
需求:登录安居客网站,在二手房板块输入"@@@",点击搜索,正确跳转成功反之失败,大部分情况下我们这样写
1 //搜索二手房 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.chrome.ChromeDriver; 6 import org.openqa.selenium.WebElement; 7 8 public class NewTest{ 9 public static void main(String[] args) throws InterruptedException { 10 11 System.setProperty ( "webdriver.chrome.driver" , 12 "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); 13 WebDriver driver = new ChromeDriver(); 14 driver.get("http://shanghai.anjuke.com"); 15 try{ 16 17 WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']")); 18 WebElement search=driver.findElement(By.xpath("//input[@id='btnSubmit']")); 19 input.sendKeys("@@@"); 20 search.click(); 21 if(driver.getTitle().contains("@")) 22 System.out.println("搜索成功,跳转到"+driver.getTitle()+"页面"); 23 else 24 System.out.println("搜索失败,跳转到了"+driver.getTitle()+"页面"); 25 26 }catch(Exception e){ 27 e.printStackTrace(); 28 }finally{ 29 Thread.sleep(3000); 30 driver.quit(); 31 } 32 }
使用Actions类还可以这样写
1 import java.util.List; 2 import java.util.Set; 3 4 import org.openqa.selenium.By; 5 import org.openqa.selenium.Keys; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.interactions.Actions; 9 import org.openqa.selenium.WebElement; 10 11 public class NewTest{ 12 public static void main(String[] args) throws InterruptedException { 13 14 System.setProperty ( "webdriver.chrome.driver" , 15 "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); 16 WebDriver driver = new ChromeDriver(); 17 driver.get("http://shanghai.anjuke.com"); 18 try{ 19 20 WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']")); 21 WebElement search=driver.findElement(By.xpath("//input[@id='btnSubmit']")); 22 23 //生成Actions实例对象 24 Actions actions=new Actions(driver); 25 //输入@@@点击搜索 26 actions.keyDown(input, Keys.SHIFT).sendKeys("222") 27 .keyUp(Keys.SHIFT).click(search).perform(); 28 29 if(driver.getTitle().contains("@")) 30 System.out.println("搜索成功,跳转到"+driver.getTitle()+"页面"); 31 else 32 System.out.println("搜索失败,跳转到了"+driver.getTitle()+"页面"); 33 34 }catch(Exception e){ 35 e.printStackTrace(); 36 }finally{ 37 Thread.sleep(3000); 38 driver.quit(); 39 } 40 }
keyDown表示按下键盘,keyUp表示松开键盘。上述代码中先是执行keyDown,这时shift键按下后面的sendKeys内容也是在shift键按下的情况输入的,所以实际输入的是@@@.
需求:登录安居客首页,切换城市到杭州
1 import org.openqa.selenium.By; 2 import org.openqa.selenium.Keys; 3 import org.openqa.selenium.WebDriver; 4 import org.openqa.selenium.chrome.ChromeDriver; 5 import org.openqa.selenium.interactions.Actions; 6 import org.openqa.selenium.WebElement; 7 8 public class NewTest{ 9 public static void main(String[] args) throws InterruptedException { 10 11 System.setProperty ( "webdriver.chrome.driver" , 12 "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); 13 WebDriver driver = new ChromeDriver(); 14 driver.get("http://shanghai.anjuke.com"); 15 try{ 16 17 WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']")); 18 WebElement citys=driver.findElement(By.xpath("//div[@id='city-panel']")); 19 WebElement cityOption=driver.findElement(By.xpath("//a[@title='杭州房产网']")); 20 //生成Actions实例对象 21 Actions actions=new Actions(driver); 22 23 //鼠标悬浮到城市标签 24 actions.moveToElement(city).perform(); 25 26 if(citys.isDisplayed()) 27 System.out.println("鼠标悬浮成功,城市模块的display:"+ 28 citys.getCssValue("display")); 29 else 30 System.out.println("鼠标悬浮失败,城市模块的display:"+ 31 citys.getCssValue("display")); 32 //点击杭州 33 actions.click(cityOption).perform(); 34 35 if(driver.getTitle().contains("杭州")) 36 System.out.println("切换成功,跳转到"+driver.getTitle()+"页面"); 37 else 38 System.out.println("切换失败,跳转到了"+driver.getTitle()+"页面"); 39 40 }catch(Exception e){ 41 e.printStackTrace(); 42 }finally{ 43 Thread.sleep(3000); 44 driver.quit(); 45 } 46 }
由于安居客网站没有这方面的例子,下面就采用YUI类库的DEMO界面来演示
1 import java.util.List; 2 import java.util.Set; 3 4 import org.openqa.selenium.By; 5 import org.openqa.selenium.Keys; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.interactions.Actions; 9 import org.openqa.selenium.WebElement; 10 11 public class NewTest{ 12 public static void main(String[] args) throws InterruptedException { 13 14 System.setProperty ( "webdriver.chrome.driver" , 15 "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); 16 WebDriver driver = new ChromeDriver(); 17 driver.get("http://jqueryui.com/selectable/"); 18 try{ 19 WebElement frame=driver.findElement(By.xpath("//iframe[@class='demo-frame']")); 20 driver.switchTo().frame(frame); 21 List<WebElement> selects=driver 22 .findElements(By.xpath("//li[@class='ui-widget-content ui-selectee']")); 23 //生成Actions实例对象 24 Actions actions=new Actions(driver); 25 actions.clickAndHold(selects.get(0)).clickAndHold(selects.get(1)).click().perform(); 26 27 }catch(Exception e){ 28 e.printStackTrace(); 29 }finally{ 30 Thread.sleep(3000); 31 driver.quit(); 32 } 33 }
clickAndHold是点击并维持着点击的状态,上面代码效果如下图
响应式网站和OA系统这方面的需求较多,下面演示如何将拖拽对象拖拽到右下角
1 import org.openqa.selenium.By; 2 import org.openqa.selenium.Keys; 3 import org.openqa.selenium.WebDriver; 4 import org.openqa.selenium.chrome.ChromeDriver; 5 import org.openqa.selenium.interactions.Actions; 6 import org.openqa.selenium.WebElement; 7 8 public class NewTest{ 9 public static void main(String[] args) throws InterruptedException { 10 11 System.setProperty ( "webdriver.chrome.driver" , 12 "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); 13 WebDriver driver = new ChromeDriver(); 14 driver.get("http://jqueryui.com/draggable/"); 15 try{ 16 WebElement frame=driver.findElement(By.xpath("//iframe[@class='demo-frame']")); 17 driver.switchTo().frame(frame); 18 WebElement dragTable=driver.findElement(By.xpath("//div[@id='draggable']")); 19 //生成Actions实例对象 20 Actions actions=new Actions(driver); 21 //拖拽对象 22 actions.dragAndDropBy(dragTable, 270, 170).perform(); 23 24 }catch(Exception e){ 25 e.printStackTrace(); 26 }finally{ 27 Thread.sleep(3000); 28 driver.quit(); 29 } 30 }
其实dragAndDropBy方法就是clickAndHold和moveByOffset方法的组合而已
actions.dragAndDropBy(dragTable, 270, 170).perform();
actions.clickAndHold(dragTable).moveByOffset(270, 170).click().perform();
这两行代码功能是一样的,正所谓条条大路通罗马,我们只需选择最近、最安全的一条就好了。