第一步:安裝eclipse
下載地址:http://www.eclipse.org/downloads/
最新版本的可能需要JDK1.7官網下載個安裝即可,注意環境變量,安裝成功後在命令行運行
java -version 結果
D:\Users\localadmin>java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) Client VM (build 24.51-b03, mixed mode, sharing)
第二步:下載selenium
Selenium:selenium-java-2.39.0.zip,下載地址:http://code.google.com/p/selenium/downloads/list
解壓後有幾個文件
三、新建一個Java Project:
然後把上面解壓出來的文件拷到新建的project目錄下,目錄結構如下圖:
添加build path,項目目錄右鍵-->Build Path--> config build path-->Java Build Path-->Libraries-->Add JARs
把libs文件夾下的jar包全部添加上,再添加selenium-java-2.39.0和selenium-java-2.39.0-srcs
添加完之後目錄結構如下圖,多了Referenced Libraries,這裏就是上面那一步添加進去的jar包:
四、eclipse裏面建測試類
package com;
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.ie.InternetExplorerDriver;
public class TestHelloWorld {
public static void main(String[] args) throws Exception {
//WebDriver driver = new FirefoxDriver();//創建火狐
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.baidu.com");//打開百度
driver.manage().window().maximize();//最大化
//driver.wait(1000);
//定位輸入框,返回一個頁面element元素對象
WebElement element = driver.findElement(By.id("kw"));
element.sendKeys("HelloWorld");//輸入值china
driver.findElement(By.id("su")).click();
driver.close();
}
}
運行即可