Selenium WebDriver java 简单实例
开发环境
JDK
下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/index.htmlEclipse:
下载地址:http://www.eclipse.org/downloads/Selenium jar包
(这里用的是:selenium-java-2.45.0.zip ,selenium-server-standalone-2.45.0.jar)
下载地址:http://code.google.com/p/selenium/downloads/list开发工具
打开Eclipse,新建一个java工程,在工程的下面新建一个lib文件夹
把selenium-java-2.45.0.zip 解压过的文件 和selenium-server-standalone-2.45.0.jar,全部复制进去
ps: 其实这些jar,放在那里无所谓,引用的时候只要给个绝对路径都是可以的,还有Selenium 对应的浏览器的版本也有严格的 要求,对于不同的火狐浏览器,selenium的版本也是不同的,如果仅仅是java项目启动webdriver,那么必须要对应版本的selenium-server-standalone-2.45.0.jar
接下来Build Path
项目目录右键–>Build Path–> config build path–>Java Build Path–>Libraries–>Add JARs
把lib下所有的jar全部添加上,包括selenium-server-standalone-2.45.0.jar,然后一路Ok,
最后会看到项目下多了一个映射的类库 Referenced Libraries
代码示例
准备工作做到这里,接下来要做的就是开始写代码了,在Src文件夹下,新建一个包,新建一个类
代码如下:
(启动浏览器,百度搜索HelloWorld,运行结束后,关闭浏览器)
package com.selenium.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class seleniumHello {
public static void main(String[] args) {
//如果火狐浏览器没有默认安装在C盘,需要制定其路径
System.setProperty("webdriver.firefox.bin", "D:/Program Files (x86)/Mozilla Firefox/firefox.exe");
//定义驱动对象为 FirefoxDriver 对象
WebDriver driver = new FirefoxDriver();
//驱动的网址
driver.get("http://www.baidu.com/");
//浏览器窗口变大
driver.manage().window().maximize();
//定位输入框元素
WebElement txtbox = driver.findElement(By.name("wd"));
//在输入框输入文本
txtbox.sendKeys("HelloWorld");
//定位按钮元素
WebElement btn = driver.findElement(By.id("su"));
//点击按钮
btn.click();
//关闭驱动
driver.close();
}
}
Selenium WebDriver java 简单实例的更多相关文章
- Selenium Webdriver java 积累一
Selenium Webdriver 学习: http://jarvi.iteye.com/category/203994 https://github.com/easonhan007/webdriv ...
- [selenium webdriver Java]常用api
1. 获取元素文本 WebElement类的getText()方法返回元素的innerText属性.所以元素里如果有子节点一样也会被返回出来.如下所示 public class GetText { @ ...
- Selenium WebDriver TestNg Maven Eclipse java 简单实例
环境准备 前提条件Eclipse 已经安装过 TestNg ,Maven 插件 新建一个普通的java项目 点击右键 configure->convert to Maven Project 之后 ...
- 【转载】Selenium WebDriver的简单操作说明
转载自:http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏 ...
- Selenium WebDriver的简单操作说明
[From] http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才 ...
- [selenium webdriver Java]隐式的等待同步
Selenium WebDriver提供了隐式等待来同步测试.当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将继续等待,超出设定时间后,抛出找不到元素异常 即,当元素没 ...
- Java 学习笔记 (二) Selenium WebDriver Java 弹出框
下面这段实例实现了以下功能: 1. profile使用用户本地电脑上的 (selenium 3有问题.因为selenium 3把profile复制到一个temp文件夹里,但并不复制回去.所以每次打开仍 ...
- Selenium webdriver Java 开始
最早接触的selenium是 selenium IDE,当时是为了准备论文.为了用IDE还下载了Firefox浏览器.后来接触过两个项目都需要selenium,一个采用selenium webdirv ...
- [selenium webdriver Java]元素定位——findElement/findElements
策略 语法 语法 描述 By id driver.findElement(By.id()) driver.findElements(By.id()) 通过id属性定位元素 By name driver ...
随机推荐
- Tomcat 内存设置
大型项目或者使用了SSH 的话, Tomcat 可能会报 java.lang.OutOfmemoryError: PermGen Space Windows: 在 bin/catlina.bat 最上 ...
- Mirantis OpenStack HA
Mysql使用Galera做Active/Active集群,同时使用Pacemaker,因为Galera mysql用到了领导机选举机制quorum,所以控制节点至少三个 RabbitMQ使用mirr ...
- C# struct 性能损失
虽然结构是值类型,但在语法上常常可以把它们当作类来处理.例如,在上面的 Dimensions 类的定义中,可以编写下面的代码:Dimensions point = new Dimensions();p ...
- vmware时间不同步的问题
请以管理员身份运行(root)
- Google Code Jam Round 1C 2015 Problem A. Brattleship
Problem You're about to play a simplified "battleship" game with your little brother. The ...
- php 原生或curl获取 http headers
有一个函数: array get_headers ( string $url [, int $format = 0 ] ) Parameters url The target URL. format ...
- Citrix 服务器虚拟化之二 Xenserver加域管理
Citrix 服务器虚拟化之二 Xenserver加域管理 如果要使用多个用户和用户组来管理XenServer服务器,就必须使用 Active Directory 用户账户进行身份验证.XenSe ...
- CString——Left、Right、Find、ReverseFind
CString--Left.Right.Find.ReverseFind http://hi.baidu.com/shawmar/item/08b30afb0f32d46f3c1485ec CStri ...
- Android:Notification的生成与取消
MainActivity.java: package com.example.notificationdemo; import android.app.Activity; import android ...
- [转]Permission denied: /.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable
原文链接:http://blog.csdn.net/dyw/article/details/6612497 近日,在Apache2环境下部署Rails3应用时碰到此错误: Permission den ...