Java+Selenium3方法篇29-Actions-划取字段【转载】

  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.firefox.FirefoxDriver;  
  10. import org.openqa.selenium.interactions.Actions;  
  11.   
  12. public class MyClass {  
  13.   
  14.     public static void main(String[] args) throws InterruptedException {  
  15.            
  16.         System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");    
  17.              
  18.         WebDriver driver = new FirefoxDriver();  
  19.           
  20.         driver.manage().window().maximize();  
  21.           
  22.         driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);  
  23.                                           
  24.         driver.get("https://www.baidu.com/duty/");  
  25.           
  26.         //定义第一段文字  
  27.         WebElement Sting_Sected = driver.findElement(By.xpath("//*/div[@class='dwri_bule']/table/tbody/tr/td/p"));  
  28.           
  29.         //定义第二段文字  
  30.         WebElement String_Second = driver.findElement(By.xpath("//*/div[@class='dwri_bule']/table/tbody/tr/td/ul/li[1]"));  
  31.           
  32.         Actions action = new Actions(driver);  
  33.         action.clickAndHold(Sting_Sected).moveToElement(String_Second).perform();  
  34.         action.release();  
  35.           
  36.     }  
  37.   
  38. }  

       在上面代码里定义了两个元素element1 和element2,element1是定位在最顶端的那段文字,element2是定义下面第一小点的文字。功能原理就是,找到顶端那个文字,然后按下鼠标不动,移动到element2然后释放鼠标,在实际过程中,会随机划取element1到element2直接的文字,划多少文字,没法确定。

posted on 2018-05-02 10:23  okeymen  阅读(62)  评论(0)    收藏  举报

导航