第十一章 SeleniumWebDriver—Actions
1.鼠标悬停Actions类
package Action;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import javax.swing.*;
public class MouseHoverActions {
WebDriver driver = new ChromeDriver();
String url = "file:///C:/Users/96510/Downloads/Compressed/1%E7%94%A8%E7%9A%84%E9%A1%B5%E9%9D%A2%E5%92%8C%E4%BB%A3%E7%A0%81/PracticePage2.html#top";
JavascriptExecutor jse;
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
Thread.sleep(8);
jse = (JavascriptExecutor) driver;
}
@Test
public void name() throws Exception {
driver.get(url);
jse.executeScript("window.scrollBy(0,600)");
Thread.sleep(2000);
WebElement webElement = driver.findElement(By.id("mousehover"));
Actions action = new Actions(driver);
action.moveToElement(webElement).perform();
driver.findElement(By.xpath("//div[@class='mouse-hover-content']/a")).click();
}
@After
public void tearDown() throws Exception {
}
}
2.怎么拖拽页面元素
package Action;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class DragAndDropActions {
WebDriver driver = new ChromeDriver();
String url = "https://jqueryui.com/droppable/";
JavascriptExecutor jse;
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
Thread.sleep(8);
jse = (JavascriptExecutor) driver;
}
@Test
public void name() throws Exception {
driver.get(url);
driver.switchTo().frame(0);
WebElement fromElement = driver.findElement(By.id("draggable"));
WebElement tolement = driver.findElement(By.id("droppable"));
Actions actions = new Actions(driver);
actions.clickAndHold(fromElement).moveToElement(tolement).release().build().perform();
}
@After
public void tearDown() throws Exception {
}
}
3.滚动条Actions
package Action;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class SlideActions {
WebDriver driver = new ChromeDriver();
String url = "https://jqueryui.com/slider/";
JavascriptExecutor jse;
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
Thread.sleep(8);
jse = (JavascriptExecutor) driver;
}
@Test
public void name() throws Exception {
driver.get(url);
driver.switchTo().frame(0);
Thread.sleep(3000);
WebElement webElement = driver.findElement(By.xpath("//div[@id='slider']/span"));
Actions actions = new Actions(driver);
actions.dragAndDropBy(webElement,100,0).perform();
}
@After
public void tearDown() throws Exception {
}
}