章節十4、9-Actions類鼠標懸停、滾動條、拖拽頁面上的元素

1、鼠標懸停html

一、在web網站中,有一些頁面元素只須要咱們將鼠標指針放在上面就會出現被隱藏的下拉框或者其它元素,在自動化的過程當中咱們使用Actions類對鼠標進行懸停操做。java

二、案例演示jquery

 1 package actionsclass;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 import org.junit.jupiter.api.AfterEach;
 6 import org.junit.jupiter.api.BeforeEach;
 7 import org.junit.jupiter.api.Test;
 8 import org.openqa.selenium.By;
 9 import org.openqa.selenium.JavascriptExecutor;
10 import org.openqa.selenium.WebDriver;
11 import org.openqa.selenium.WebElement;
12 import org.openqa.selenium.chrome.ChromeDriver;
13 import org.openqa.selenium.interactions.Actions;
14 
15 class MouseHoverActions {
16 
17     WebDriver driver;
18     String url;
19     JavascriptExecutor jsp;
20     
21     @BeforeEach
22     void setUp() throws Exception {
23         driver = new ChromeDriver();
24         url = "file:///C:/Users/acer/Desktop/%E5%85%B6%E5%AE%83/PracticePage2.html";
25         jsp = (JavascriptExecutor)driver;
26         driver.manage().window().maximize();
27         driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
28         driver.get(url);
29     }
30 
31     @Test
32     void test() throws InterruptedException {
33         jsp.executeScript("window.scrollBy(0,600)");
34         Thread.sleep(2000);
35         WebElement subElement = driver.findElement(By.id("mousehover"));
36 //        new一個actions對象
37         Actions action = new Actions(driver);
38 //        鼠標懸停在指定的元素上
39         action.moveToElement(subElement).perform();
40         Thread.sleep(2000);
41 //        操做懸停後的元素
42         WebElement clickElement = driver.findElement(By.linkText("回到頂部"));
43 //        經過Actions類來執行點擊操做
44         action.moveToElement(clickElement).click().perform();
45     }
46     
47     @AfterEach
48     void tearDown() throws Exception {
49         Thread.sleep(2000);
50         driver.quit();
51     }
52 }

 

2、拖拽頁面上的元素web

一、在web頁面中,有時咱們須要將元素從一個固定的位置經過鼠標拖拽到另外一個位置,自動化的過程當中咱們能夠經過Actions類來實現拖拽功能。chrome

二、案例演示api

 1 package actionsclass;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 import org.junit.jupiter.api.AfterEach;
 6 import org.junit.jupiter.api.BeforeEach;
 7 import org.junit.jupiter.api.Test;
 8 import org.openqa.selenium.By;
 9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.WebElement;
11 import org.openqa.selenium.chrome.ChromeDriver;
12 import org.openqa.selenium.interactions.Actions;
13 
14 class DragAndDropActions {
15     
16     WebDriver driver;
17     String url;
18 
19     @BeforeEach
20     void setUp() throws Exception {
21         driver = new ChromeDriver();
22         url = "https://jqueryui.com/droppable/";
23         driver.manage().window().maximize();
24         driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
25         driver.get(url);
26     }
27 
28     @Test
29     void test() {
30         driver.switchTo().frame(0);
31         WebElement fromElement = driver.findElement(By.id("draggable"));
32         WebElement toElement = driver.findElement(By.id("droppable"));
33         Actions action = new Actions(driver);
34         
35 //        方法一:直接拖拽
36 //        action.dragAndDrop(fromElement, toElement).build().perform();
37         
38 //        方法2、分步驟進行拖拽
39 //        一、clickAndHold:點擊須要拖拽的元素
40 //        二、moveToElement:移動到指定元素位置
41 //        三、release:鬆開鼠標
42         action.clickAndHold(fromElement).moveToElement(toElement).release().build().perform();
43     }
44     
45     @AfterEach
46     void tearDown() throws Exception {
47         Thread.sleep(2000);
48         driver.quit();
49     }
50 }

 

 三,滾動條jsp

 1 package actionsclass;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 import org.junit.jupiter.api.AfterEach;
 6 import org.junit.jupiter.api.BeforeEach;
 7 import org.junit.jupiter.api.Test;
 8 import org.openqa.selenium.By;
 9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.WebElement;
11 import org.openqa.selenium.chrome.ChromeDriver;
12 import org.openqa.selenium.interactions.Actions;
13 
14 class SliderActions {
15 
16     WebDriver driver;
17     String url;
18     
19     @BeforeEach
20     void setUp() throws Exception {
21         driver = new ChromeDriver();
22         url = "https://jqueryui.com/slider/";
23         driver.manage().window().maximize();
24         driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
25         driver.get(url);
26     }
27 
28     @Test
29     void test() throws InterruptedException {
30         driver.switchTo().frame(0);
31         Thread.sleep(2000);
32         WebElement element = driver.findElement(By.xpath("//div[@id='slider']/span"));
33         Actions action = new Actions(driver);
34 //        橫向移動滾動條
35         action.dragAndDropBy(element, 100, 0).perform();
36     }
37     
38     @AfterEach
39     void tearDown() throws Exception {
40         Thread.sleep(2000);
41         driver.quit();
42     }
43 }

 

若是有不明白的小夥伴能夠加羣「555191854」問我,羣裏都是軟件行業的小夥伴相互一塊兒學習。ide

內容具備連慣性,未標註的地方能夠看前面的博客,這是一整套關於ava+selenium自動化的內容,從java基礎開始。學習

歡迎關注,轉載請註明來源。網站

相關文章
相關標籤/搜索