java工作复习——鼠标事件——action类——转载总结

package com.test.mouse;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;

public class MouseOperation {

    public static void main(String[] args) {
        FirefoxProfile profile = new FirefoxProfile(
                new File("C:\\Users\\XXXX\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\a6xwo0b1.default"));
        WebDriver driver = new FirefoxDriver(profile);

        driver.get("http://c37.yunpan.360.cn");
        driver.manage().window().maximize();
        waitTime(5000);

        driver.findElement(By.xpath("//*[@id='infoPanel']/a[2]")).click();
        waitTime(3000);

        driver.findElement(By.xpath("//*[@id='tbText']")).click();
        WebElement testitem = driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[2]"));
        testitem.click();
        waitTime(3000);

        // 左击实现(和元素的click类似)
        Actions action = new Actions(driver);
        WebElement test1item = driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[2]"));
        action.click(test1item).perform();
        waitTime(5000);

        // 返回上一级
        driver.findElement(By.xpath("//*[@id='crumb']/div/span[1]")).click();
        waitTime(5000);

        // 双击实现
        new Actions(driver).doubleClick(driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[2]"))).perform();
        waitTime(5000);

        // 返回上一级
        driver.findElement(By.xpath("//*[@id='crumb']/div/span[1]")).click();
        waitTime(5000);

        // 悬停 到更多按钮实现
        new Actions(driver).moveToElement(driver.findElement(By.xpath("//*[@id='topPanel']/ul/li[3]/a"))).perform();

        // 拖动实现
        driver.findElement(By.xpath("//*[@id='tbPic']")).click();
        WebElement begin = driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[1]"));
        WebElement end = driver.findElement(By.xpath("//*[@id='list']/li[2]/div[2]/span[1]"));
        new Actions(driver).dragAndDrop(begin, end).perform();

        // 右击实现
        // 这里虽然使用的是元素任然是test1item,但是页面刷新过后需要重新定位
        // 参考http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp
        driver.findElement(By.xpath("//*[@id='tbText']")).click();
        new Actions(driver).contextClick(driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[2]")))
                .perform();
        waitTime(5000);

        driver.findElement(By.xpath("//*[@id='x-yp-3']/ul/li[4]/a/span")).click();
        waitTime(5000);

        driver.quit();

    }

    static public void waitTime(int time) {

        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值