标签:blog http io ar os sp java on 文件
转之博文:http://www.cnblogs.com/puresoul/p/3483055.html
运行碰到报错:Webdriver Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms 更新selenium-java.jar包到最新;
*开发环境:
1.JDK1.7
2.Eclipse,下载地址:http://www.eclipse.org/downloads/
3.selenium-java-2.44.0.jar,下载地址:http://docs.seleniumhq.org/download/
解压selenium-java,如下图所示:
4.firefox 33.1
*Eclipse:
1.新建一个Java Project:把selenium-java解压出来文件拷贝到新建Project中;
2.添加build path,项目目录右键-->Build Path--> config build path-->Java Build Path-->Libraries-->Add External JARs
把libs文件夹下的jar包全部添加上,再添加selenium-java-2.44.0和selenium-java-2.44.0-srcs
3.添加完之后目录结构如下图,多了Referenced Libraries,这里就是上面那一步添加进去的jar包
4.关联webdriver的源码:展开Referenced Libraries文件夹,选择selenium-java-2.44.0.jar,右击Properties-->Java Source Attachment-->External location 选择selenium-java-2.44.0-srcs.jar所在路径(就是当前Project的路径);
至此环境就配置完成了;
*示例:
1.在src下新建测试类:新建测试包com.selenium.demo-->新建测试类TestSelenium.java
2.代码如下:
package com.selenium.Glen;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class TestHelloWorld {
public static void main(String[] args) {
//如果火狐浏览器没有默认安装在C盘,需要制定其路径
//System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe");
WebDriver driver = new FirefoxDriver();
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();
}
}
然后直接右键-->Run As-->Java Application就可以看到效果了。
Selenium2(webdirver)入门之环境搭建(Java版)
标签:blog http io ar os sp java on 文件
原文地址:http://www.cnblogs.com/JoykingXu0814/p/4137987.html