第十二章 SeleniumWebDriver—Actions

本文档展示了如何使用Selenium WebDriver的Actions类来实现鼠标悬停、页面元素拖拽以及滚动条操作。通过Java编程,演示了在Chrome浏览器中定位元素并执行鼠标悬停、拖放和滑动滚动条的动作,适用于自动化测试和网页交互模拟。
摘要由CSDN通过智能技术生成

第十一章 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.*;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
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 {
//            driver.quit();
    }

}
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;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
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.dragAndDrop(fromElement,tolement).build().perform();
        //第二种方法
        actions.clickAndHold(fromElement).moveToElement(tolement).release().build().perform();
    }

    @After
    public void tearDown() throws Exception {
//            driver.quit();
    }
}
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;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
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 {
//            driver.quit();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值