-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
41 lines (38 loc) · 1.25 KB
/
test.js
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
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
/**
* @author youthflies yeetrack.com
* Mytest.java 2013-5-19
* 测试所用临时文件
*/
public class Mytest
{
public static void main(String[] args) throws InterruptedException
{
//可能需要设置firefox的路径
WebDriver driver = new FirefoxDriver();
try
{
driver.get("http://www.baidu.com");
//利用webdriver键入搜索关键字
//driver.findElement(By.id("kw")).sendKeys("yeetrack");
//利用js代码键入搜索关键字
((JavascriptExecutor)driver).executeScript("document.getElementById(\"kw\").value=\"yeetrack\"");
//利用js代码取出关键字
String keyword = (String) ((JavascriptExecutor)driver).executeScript("var input = document.getElementById(\"kw\").value;return input");
System.out.println(keyword);
driver.findElement(By.id("su")).click();
TimeUnit.SECONDS.sleep(5);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
driver.quit();
}
}
}