selenium测试(Java)--鼠标事件(六)

时间:2023-03-09 16:52:09
selenium测试(Java)--鼠标事件(六)

鼠标操作:

1.右击

2.双击

3.拖到

4.悬停

selenium测试(Java)--鼠标事件(六)
 1 package com.test.mouse;
2
3 import java.io.File;
4
5 import org.openqa.selenium.By;
6 import org.openqa.selenium.WebDriver;
7 import org.openqa.selenium.WebElement;
8 import org.openqa.selenium.firefox.FirefoxDriver;
9 import org.openqa.selenium.firefox.FirefoxProfile;
10 import org.openqa.selenium.interactions.Actions;
11
12 public class MouseOperation {
13
14 public static void main(String[] args) {
15 FirefoxProfile profile = new FirefoxProfile(
16 new File("C:\\Users\\XXXX\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\a6xwo0b1.default"));
17 WebDriver driver = new FirefoxDriver(profile);
18
19 driver.get("http://c37.yunpan.360.cn");
20 driver.manage().window().maximize();
21 waitTime(5000);
22
23 driver.findElement(By.xpath("//*[@id='infoPanel']/a[2]")).click();
24 waitTime(3000);
25
26 driver.findElement(By.xpath("//*[@id='tbText']")).click();
27 WebElement testitem = driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[2]"));
28 testitem.click();
29 waitTime(3000);
30
31 // 左击实现(和元素的click类似)
32 Actions action = new Actions(driver);
33 WebElement test1item = driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[2]"));
34 action.click(test1item).perform();
35 waitTime(5000);
36
37 // 返回上一级
38 driver.findElement(By.xpath("//*[@id='crumb']/div/span[1]")).click();
39 waitTime(5000);
40
41 // 双击实现
42 new Actions(driver).doubleClick(driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[2]"))).perform();
43 waitTime(5000);
44
45 // 返回上一级
46 driver.findElement(By.xpath("//*[@id='crumb']/div/span[1]")).click();
47 waitTime(5000);
48
49 // 悬停 到更多按钮实现
50 new Actions(driver).moveToElement(driver.findElement(By.xpath("//*[@id='topPanel']/ul/li[3]/a"))).perform();
51
52 // 拖动实现
53 driver.findElement(By.xpath("//*[@id='tbPic']")).click();
54 WebElement begin = driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[1]"));
55 WebElement end = driver.findElement(By.xpath("//*[@id='list']/li[2]/div[2]/span[1]"));
56 new Actions(driver).dragAndDrop(begin, end).perform();
57
58 // 右击实现
59 // 这里虽然使用的是元素任然是test1item,但是页面刷新过后需要重新定位
60 // 参考http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp
61 driver.findElement(By.xpath("//*[@id='tbText']")).click();
62 new Actions(driver).contextClick(driver.findElement(By.xpath("//*[@id='list']/li[1]/div[2]/span[2]")))
63 .perform();
64 waitTime(5000);
65
66 driver.findElement(By.xpath("//*[@id='x-yp-3']/ul/li[4]/a/span")).click();
67 waitTime(5000);
68
69 driver.quit();
70
71 }
72
73 static public void waitTime(int time) {
74
75 try {
76 Thread.sleep(time);
77 } catch (InterruptedException e) {
78 // TODO Auto-generated catch block
79 e.printStackTrace();
80 }
81 }
82
83 }
selenium测试(Java)--鼠标事件(六)

注:perform()的作用是 执行所有Actions中存储的行为。