码迷,mamicode.com
首页 > 编程语言 > 详细

windows7 selenium2+java 搭建

时间:2016-11-24 14:35:45      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:file   ram   新建工程   方便   drive   exception   低版本   exce   ring   

一、环境配置

1、下载selenium-java-2.53.0,并解压,下载selenium-server-standalone-2.53.1.jar(官网下载)

2、下载eclipse(Version: Neon.1a Release (4.6.1)) 官网下载的最新版

3、安装jdk(1.8版本) ,我之前装的是1.7,不支持eclipse最新版,只好卸载重装了1.8

4、firefox 45(要使用较低版本的firefox,否则与selenium2不兼容,导致无法掉起)

二、创建工程

1、打开eclipse->新建工程tests

2、导入selenium-java-2.53.0.jar、selenium-server-standalone-2.53.1.jar(步骤:工程名右键->Build path->Add Libraries)

3、新建类Seleniumcn,代码如下

package tests;

import org.openqa.selenium.By; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 

import org.openqa.selenium.firefox.FirefoxDriver; 

public class Seleniumcn {

	public	static void main(String[] args) { 
 
		System.setProperty ("webdriver.firefox.bin","C:/Program Files (x86)/Mozilla Firefox/firefox.exe" ); 
             WebDriver driver = new FirefoxDriver(); 
             driver.get("http://www.baidu.com"); 
             WebElement element = driver.findElement(By.name("wd")); 
             element.sendKeys("hello Selenium!");  
             element.submit();
             try {
            	 Thread.sleep(3000);
             } catch (InterruptedException e) {
            	 e.printStackTrace();
             }
             System.out.println( "Page title is: "+ driver.getTitle());//把页面title打印出来,方便知晓是否完成搜索
             driver.quit();
}
}

 三、运行

1、打开firefox,打开百度,输入hello Selenium!

2、运行结果:Page title is: hello Selenium!_百度搜索

 

windows7 selenium2+java 搭建

标签:file   ram   新建工程   方便   drive   exception   低版本   exce   ring   

原文地址:http://www.cnblogs.com/sherri/p/6097060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!