Webdriver API (一)
(转载)
1.1 下载selenium2.0的包
- 官方download包地址:http://code.google.com/p/selenium/downloads/list
- 官方User Guide: http://seleniumhq.org/docs/
- 官方API: http://selenium.googlecode.com/git/docs/api/java/index.html
1.2.1 用webdriver打开一个浏览器
- 打开firefox浏览器:
WebDriver driver = new FirefoxDriver();
如果firefox不是默认安装路径,需要指定它的路径再启动:
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver webDriver = new FirefoxDriver();
- 打开IE浏览器
WebDriver driver = new InternetExplorerDriver ();
也可以指定准确的IE路径,然后启动它:
System.setProperty("webdriver.ie.driver",
"C:/Program Files (x86)/Internet Explorer/iexplore.exe");
WebDriver driver = new InternetExplorerDriver();
- 打开HtmlUnit浏览器
WebDriverdriver = new HtmlUnitDriver();
- 打开chrome浏览器
WebDriverdriver = new ChromeDriver();
1.2.2 最大化浏览器
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
1.2.3 关闭浏览器
WebDriver driver = new FirefoxDriver();
- driver.close();
- driver.quit();
1.3 打开测试页面
- driver.get("http://www.google.com");
- driver.navigate().to("http://www.baidu.com/");
P.S.navigate方法会产生1个Navigator对象,其封装了与导航相关的一些方法,比如前进后退等
navigate()进行页面跳转时不会进行页面数据的校验(比如当前页面有必填项但是实际上没填,navigate也会跳到to页面),相反get()则可以进行这些校验,更真实的模拟实际情况
1.4 页面元素定位
Webdriver提供下面两种方法来定位页面元素,参数是By对像,最常用是By.id和By.name查找。
- findElement 定位某个元素,如果没有找到元素会抛出异常:NoSuchElementException
- findElements 定位一组元素
例如需要定位如下元素:
<input class="input_class" type="text" name="passwd" id="passwd-id" />
- By.id:
WebElement element = driver.findElement(By.id("passwd-id"));
- By.name:
WebElement element = driver.findElement(By.name("passwd"));
- By.xpath:
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
- By.className
WebElement element = driver.findElement(By.className("input_class"));
- By.cssSelector
WebElement element = driver.findElement(By.cssSelector(".input_class"));
- By.linkText:
//通俗点就是精确查询
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.linkText("百科"));
- By.partialLinkText:
//这个方法就是模糊查询
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.partialLinkText("hao"));
- By.tagName:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
String test= driver.findElement(By.tagName("form")).getAttribute("name");
System.out.println(test);
小技巧:当使用某一属性识别元素时如果返回了多个,可通过先找到唯一的父元素,然后再找他的子元素,如下,
WebElement web=driver.findElement(By.id("form")).findElement(By.id("kw"));
1.5 如何对页面元素进行操作
1.5.1 输入框(text field or textarea)
WebElement element = driver.findElement(By.id("passwd-id"));
- element.sendKeys(“test”);//在输入框中输入内容:
- element.clear(); //将输入框清空
- element.getText(); //获取输入框的文本内容:
1.5.2下拉选择框(Select)
Select select = new Select(driver.findElement(By.id("select")));
- select.selectByVisibleText(“A”);
- select.selectByValue(“1”);
- select.deselectAll();
- select.deselectByValue(“1”);
- select.deselectByVisibleText(“A”);
- select.getAllSelectedOptions();
- select.getFirstSelectedOption();
1.5.3单选项(Radio Button)
WebElement radio=driver.findElement(By.id("BookMode"));
- radio.click(); //选择某个单选项
- radio.clear(); //清空某个单选项
- radio.isSelected(); //判断某个单选项是否已经被选择
1.5.4多选项(checkbox)
WebElement checkbox = driver.findElement(By.id("myCheckbox."));
- checkbox.click();
- checkbox.clear();
- checkbox.isSelected();
- checkbox.isEnabled();
1.5.5按钮(button)
WebElement btn= driver.findElement(By.id("save"));
- btn.click(); //点击按钮
- btn.isEnabled (); //判断按钮是否enable
1.5.7弹出对话框(Popup dialogs)
Alert alert = driver.switchTo().alert();
- alert.accept(); //确定
- alert.dismiss(); //取消
- alert.getText(); //获取文本
1.5.8表单(Form)
Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
或
approve.submit();//只适合于表单的提交
1.5.9上传文件
上传文件的元素操作:
WebElement adFileUpload =driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
1.6 Windows 和 Frames之间的切换
- driver.switchTo().defaultContent(); //返回到最顶层的frame/iframe
- driver.switchTo().frame("leftFrame"); //切换到某个frame:
- driver.switchTo().window("windowName"); //切换到某个window
1.7 调用Java Script
Web driver对Java Script的调用是通过JavascriptExecutor来实现的,例如:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("JS脚本");
1.8 超时设置
WebDriver driver = new FirefoxDriver();
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //识别元素时的超时时间,影响范围:全局。(慎用)
- driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); //页面加载时的超时时间
- driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS); //异步脚本的超时时间
Webdriver API (一)的更多相关文章
- 转:python webdriver API 之操作测试对象
一般来说,所有有趣的操作与页面交互都将通过 WebElement 接口,包括上一节中介绍的对象定位,以及本节中需要介绍的常对象操作.webdriver 中比较常用的操作元素的方法有下面几个: cle ...
- Webdriver API (二)
(转载) 1.3 打开测试页面 对页面对测试,首先要打开被测试页面的地址(如:http://www.google.com),web driver 提供的get方法可以打开一个页面: // And no ...
- selenium2(WebDriver) API
selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/ 1.1 下载selenium2.0的包 官方downl ...
- Selenium2+Python:Webdriver API速记手册
由于web自动化常常需要控制浏览器行为和操作页面元素,相关函数又比较多,于是再此记下一份Webdriver API查阅文档以备不时之需. 参考:虫师<Selenium2自动化测试实战>,和 ...
- webdriver API中文文档
1.1 下载selenium2.0的lib包 http://code.google.com/p/selenium/downloads/list 官方UserGuide:http://seleniu ...
- python+selenium自动化软件测试(第2章):WebDriver API
2.1 操作元素基本方法 前言前面已经把环境搭建好了,从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可 ...
- Selenium WebDriver Api 知识梳理
之前一直没有系统的梳理WebDriver Api的相关知识,今天借此机会整理一下. 1.页面元素定位 1.1.8种常用定位方法 # id定位 driver.find_element_by_id() # ...
- 2.28 查看webdriver API
2.28 查看webdriver API(带翻译) 前言 前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就教大家如何去查看seleni ...
- python2.7运行selenium webdriver api报错Unable to find a matching set of capabilities
在火狐浏览器33版本,python2.7运行selenium webdriver api报错:SessionNotCreatedException: Message: Unable to find a ...
- Webdriver API中文版
Webdriver API中文版 1.1 下载selenium2.0的lib包 http://code.google.com/p/selenium/downloads/list 官方UserGui ...
随机推荐
- MySQL中字符串函数详细介绍
MySQL字符串函数对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str)返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回N ...
- 在 Windows 8 或 8.1 上安装 .NET Framework 3.5 安装错误:0x800f0906、0x800F081F
昨天给一天新装Windows 8.1的PC装.NET Framework 3.5 发现联网速度很慢,并且在长久等待过后直接报错了:0x800f0906 经过Bing,发现了解决方案: 如果根据需要安装 ...
- SQL 去除重复、获取最新记录
应用中常会有需要去除重复的记录,或者获取某些最新记录(如:每个用户可以答题多次,每次答题时间不同,现在要获取所有用户的最新答题记录,即每个用户取最新的一条) 使用group 和max 即可实现上述功能 ...
- mac下安装应用及常用快捷键
从网络上下载的应用程序如何安装? 主要分类为两种:(dmg 和 pkg) 1.dmg类型 此类应用程序安装非常简单,只需要双击图标,然后将此应用程序图标直接拖拽到 application图标上即可 ...
- [转载]VS2012创建MVC3项目提示错误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。
如果在没有安装vs2012 update3升级包的情况下,创建MVC3项目会出现下面的错误信息. 因为VS2012已经全面切换到使用NuGet这个第三方开源工具来管理项目包和引用模块了,使用VS201 ...
- Flume学习——Flume中事务的定义
首先要搞清楚的问题是:Flume中的事务用来干嘛? Flume中的事务用来保证消息的可靠传递. 当使用继承自BasicChannelSemantics的Channel时,Flume强制在操作Chann ...
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简介
http://www.cnblogs.com/kflwz/articles/1337310.html 1.下载最新版FreeTextBox(版本3.1.6),解压 FreeTextBox 3. ...
- jstack(查看线程)、jmap(查看内存)和jstat(性能分析)
公司内部同事分享的一篇文章 周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令.供大家参考 1.Jstack 1.1 ...
- SQLite入门与分析(三)---内核概述(2)
写在前面:本节是前一节内容的后续部分,这两节都是从全局的角度SQLite内核各个模块的设计和功能.只有从全局上把握SQLite,才会更容易的理解SQLite的实现.SQLite采用了层次化,模块化的设 ...
- ANDROID_MARS学习笔记_S01_006ImageView
一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...