环境配置:

1.chrome:和对应的chromedriver.exe

2.jdk版本1.8

3.selenium-standalone.jar

常见的定位:xpath、className、linkText

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

@SuppressWarnings("unused")
public class Example {

// Mooctest Selenium Example


// <!> Check if selenium-standalone.jar is added to build path.

public static void test(WebDriver driver) {
// TODO Test script
// eg:driver.get("https://www.baidu.com/")
// eg:driver.findElement(By.id("wd"));


try {

driver.get("");//打开网页
driver.manage().window().maximize();//最大化
Thread.sleep(1500);//等待1.5秒
driver.findElement(By.linkText("电影")).click();//点击“电影”
Thread.sleep(1500);
//切换网页界面,标识从0开始
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
Thread.sleep(1500);
//点击 class=“curr”
driver.findElement(By.className("curr")).click();
Thread.sleep(1500);
driver.switchTo().window(driver.getWindowHandles().toArray()[2].toString());//
Thread.sleep(1500);
driver.findElement(By.linkText("免费")).click();//3
Thread.sleep(1500);
//driver.findElement(By.linkText("喜剧")).click();//6
//Thread.sleep(1500);
//driver.findElement(By.linkText("爱情")).click();//7
//Thread.sleep(1500);
driver.findElement(By.linkText("动作")).click();//4
Thread.sleep(1500);
driver.findElement(By.linkText("中国香港")).click();//5
Thread.sleep(1500);
driver.findElement(By.linkText("2016")).click();//6
Thread.sleep(1500);
driver.findElement(By.linkText("最新")).click();//7
Thread.sleep(1500);
//通过xpath定位 driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/dl[1]/dd[1]/a/i")).click();
Thread.sleep(1500);
driver.switchTo().window(driver.getWindowHandles().toArray()[3].toString());
Thread.sleep(1500);
driver.findElement(By.className("search_input")).sendKeys("周星驰");//9
Thread.sleep(1500);
driver.findElement(By.className("search_btn")).click();//10
Thread.sleep(1500);
//driver.findElement(By.linkText("展开更多")).click();//11
//Thread.sleep(1500);
driver.findElement(By.linkText("下一页")).click();//12
Thread.sleep(1500);
driver.findElement(By.linkText("首页")).click();//13
Thread.sleep(1500);
driver.findElement(By.linkText("电视剧")).click();//14
Thread.sleep(1500);
driver.switchTo().window(driver.getWindowHandles().toArray()[4].toString());
Thread.sleep(1500);
driver.findElement(By.linkText("更多")).click();//15
Thread.sleep(1500);
driver.switchTo().window(driver.getWindowHandles().toArray()[5].toString());
Thread.sleep(1500);
driver.findElement(By.linkText("独播")).click();//16
Thread.sleep(1500);
driver.findElement(By.linkText("穿越")).click();//17
Thread.sleep(1500);
driver.findElement(By.linkText("2013")).click();//18
Thread.sleep(1500);
driver.findElement(By.className("d_img")).click();//19
Thread.sleep(1500);
driver.switchTo().window(driver.getWindowHandles().toArray()[6].toString());
Thread.sleep(1500);
driver.findElement(By.linkText("预告片")).click();//20
Thread.sleep(1500);
driver.findElement(By.linkText("花絮")).click();//21
Thread.sleep(1500);
driver.findElement(By.linkText("分集剧情")).click();//22
Thread.sleep(1500);
driver.findElement(By.linkText("乐迷畅谈")).click();//23
Thread.sleep(1500);


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




}

public static void main(String[] args) {
// Run main function to test your script.
WebDriver driver = new ChromeDriver();
try { test(driver); }
catch(Exception e) { e.printStackTrace(); }
finally { driver.quit(); }
}

}