用 Eclipse 建个 Maven 的工程,建成后,直接修改 pom.xml,(参考:http://seleniumhq.org/docs/03_webdriver.html#setting-up-a-selenium-webdriver-project) html
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Selenium2Test</groupId> <artifactId>Selenium2Test</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.25.0</version> </dependency> <dependency> <groupId>com.opera</groupId> <artifactId>operadriver</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.opera</groupId> <artifactId>operadriver</artifactId> <version>0.16</version> <exclusions> <exclusion> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> </project>
pom.xml 修改保存后,eclipse 会自动把须要的 jar 包下载完成。 前端
package lesson1; 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.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForFireFox { public static void main(String[] args) { // 若是你的 FireFox 没有安装在默认目录,那么必须在程序中设置 // System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe"); // 建立一个 FireFox 的浏览器实例 WebDriver driver = new FirefoxDriver(); // 让浏览器访问 Baidu driver.get("http://www.baidu.com"); // 用下面代码也能够实现 // driver.navigate().to("http://www.baidu.com"); // 获取 网页的 title System.out.println("1 Page title is: " + driver.getTitle()); // 经过 id 找到 input 的 DOM WebElement element = driver.findElement(By.id("kw")); // 输入关键字 element.sendKeys("zTree"); // 提交 input 所在的 form element.submit(); // 经过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().endsWith("ztree"); } }); // 显示搜索结果页面的 title System.out.println("2 Page title is: " + driver.getTitle()); //关闭浏览器 driver.quit(); } }
出现这个错误,颇有意思。 查了一下 有人说应该是 hosts 出现了问题,加上一个 127.0.0.1 localhost 就好了,但个人 hosts 上确定有这个玩意,为啥也会出现这个问题呢? java
通过调试,发现 127.0.0.1 localhost 的设置必需要在 hosts 文件的最开始,并且若是后面有其余设置后,也不要再出现一样的 127.0.0.1 localhost ,只要有就会出错。(由于我为了方便访问 google 的网站,专门加入了 smarthosts 的内容,致使了 localhost 的重复) web
【3. 测试 Chrome】package lesson1; import java.io.File; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForChrome { public static void main(String[] args) throws IOException { // 设置 chrome 的路径 System.setProperty( "webdriver.chrome.driver", "C:\\Documents and Settings\\sq\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe"); // 建立一个 ChromeDriver 的接口,用于链接 Chrome @SuppressWarnings("deprecation") ChromeDriverService service = new ChromeDriverService.Builder() .usingChromeDriverExecutable( new File( "E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe")) .usingAnyFreePort().build(); service.start(); // 建立一个 Chrome 的浏览器实例 WebDriver driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome()); // 让浏览器访问 Baidu driver.get("http://www.baidu.com"); // 用下面代码也能够实现 // driver.navigate().to("http://www.baidu.com"); // 获取 网页的 title System.out.println("1 Page title is: " + driver.getTitle()); // 经过 id 找到 input 的 DOM WebElement element = driver.findElement(By.id("kw")); // 输入关键字 element.sendKeys("zTree"); // 提交 input 所在的 form element.submit(); // 经过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().endsWith("ztree"); } }); // 显示搜索结果页面的 title System.out.println("2 Page title is: " + driver.getTitle()); // 关闭浏览器 driver.quit(); // 关闭 ChromeDriver 接口 service.stop(); } }
【2012.12.06补充】 chrome
上一个 Demo 中没法正常使用 new ChromeDriver(),今天在作进一步学习时看到一篇文章(http://qa.blog.163.com/blog/static/19014700220122231779/),恍然大悟,原来只须要把 ‘webdriver.chrome.driver’ 的值设置为 chromedriver 的路径就能够了。 apache
package lesson1; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForChrome2 { public static void main(String[] args) throws IOException { // 设置 chrome 的路径 System.setProperty( "webdriver.chrome.driver", "E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe"); // 建立一个 ChromeDriver 的接口,用于链接 Chrome // 建立一个 Chrome 的浏览器实例 WebDriver driver = new ChromeDriver(); // 让浏览器访问 Baidu driver.get("http://www.baidu.com"); // 用下面代码也能够实现 // driver.navigate().to("http://www.baidu.com"); // 获取 网页的 title System.out.println("1 Page title is: " + driver.getTitle()); // 经过 id 找到 input 的 DOM WebElement element = driver.findElement(By.id("kw")); // 输入关键字 element.sendKeys("zTree"); // 提交 input 所在的 form element.submit(); // 经过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().endsWith("ztree"); } }); // 显示搜索结果页面的 title System.out.println("2 Page title is: " + driver.getTitle()); // 关闭浏览器 driver.quit(); // element = driver.findElement(By.id("kw")); // // element.clear(); // element.click(); // element.clear(); // element.sendKeys("zTree"); // element.submit(); } }
package lesson1; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForIE { public static void main(String[] args) { // 若是你的 FireFox 没有安装在默认目录,那么必须在程序中设置 // System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe"); // 建立一个 FireFox 的浏览器实例 WebDriver driver = new InternetExplorerDriver(); // 让浏览器访问 Baidu driver.get("http://www.baidu.com"); // 用下面代码也能够实现 // driver.navigate().to("http://www.baidu.com"); // 获取 网页的 title System.out.println("1 Page title is: " + driver.getTitle()); // 经过 id 找到 input 的 DOM WebElement element = driver.findElement(By.id("kw")); // 输入关键字 element.sendKeys("zTree"); // 提交 input 所在的 form element.submit(); // 经过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().endsWith("ztree"); } }); // 显示搜索结果页面的 title System.out.println("2 Page title is: " + driver.getTitle()); // 关闭浏览器 driver.quit(); } }