一、开发环境:
1、JDK1.8:下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html
2、Eclipse:下最新版,下载地址:http://www.eclipse.org/downloads/
3、selenium-Java-2.45.0.zip,下载地址:http://code.google.com/p/selenium/downloads/list
二、新建一个Java Project:
1、新建一个Java Project后,添加build path,项目目录右键–>Build Path–> config build path–>Java Build Path–>Libraries–>Add External JARs
把libs文件夹下的jar包全部添加上,再添加selenium-java-2.45.0和selenium-java-2.45.0-srcs
2、添加完之后目录结构如下图,多了Referenced Libraries,这里就是上面那一步添加进去的jar包:
至此,环境工作准备就绪,下面来写一个简单的小例子。
三、在src下面新建测试类,如下图:
代码如下,主要是打开百度,然后在搜索框输入selenium,点击搜索按钮,关闭浏览器。
package com.selenium.demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//chromedriver.exe也可放入C:\Windows\System32路径中。
System.setProperty("webdriver.chrome.driver","D:\\software\\test\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
driver.manage().window().maximize();
WebElement txtbox = driver.findElement(By.name("wd"));
txtbox.sendKeys("selenium");
WebElement btn = driver.findElement(By.id("su"));
btn.click();
driver.close();
}
}