Java+Selenium3方法篇33-Actions-拖拽操作【转载】

本文补充Actions类中的拖拽操作,之前没有找到相应的demo网站来介绍拖拽操作,今天找到了一个,所以,补上一个Selenium中处理拖拽的场景例子。拖拽的动作定义是,一个元素从起始位置拖动到目标位置,这个过程叫拖拽。直接来看看下面的脚本实现过程。

[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.WebDriver;  
  7. import org.openqa.selenium.WebElement;  
  8. import org.openqa.selenium.chrome.ChromeDriver;  
  9. import org.openqa.selenium.interactions.Actions;  
  10.   
  11. public class ElementOpration {  
  12.     public static void main(String[] args) throws Exception {    
  13.           
  14.         System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");    
  15.              
  16.         WebDriver driver = new ChromeDriver();    
  17.        
  18.         driver.manage().window().maximize();    
  19.          
  20.         driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);  
  21.             
  22.         driver.get("http://jqueryui.com/resources/demos/droppable/default.html");    
  23.          
  24.         Thread.sleep(2000);  
  25.           
  26.         // 定位能拖拽的元素  
  27.         WebElement move_ele = driver.findElement(By.id("draggable"));  
  28.           
  29.         // 定位拖拽目标位置元素  
  30.         WebElement target_ele = driver.findElement(By.id("droppable"));  
  31.           
  32.         Actions action = new Actions(driver);  
  33.         action.dragAndDrop(move_ele, target_ele).build().perform();  
  34.           
  35.         // 验证拖拽成功  
  36.         assert(driver.findElement(By.xpath("//*[@id='droppable']/p[text()='Dropped!']")).isDisplayed() == true);  
  37.         
  38.       }  
  39. }  

上面刚刚,拖拽后第二个元素内部的文字有变化,我们通过获取文字变化判断是否显示在该位置来断言拖拽是否成功。isDisplayed方法,前面文章介绍过。

posted on 2018-05-02 13:37  okeymen  阅读(153)  评论(0)    收藏  举报

导航