Selenium2(WebDriver)总结(四)---基本元素操作
WebDriver提供了常用的WEB控件的操作方法,比如:按钮、输入框、超链接等,废话不多说,直接上代码:
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.Select; public class WebDriverSimple { WebDriver driver; public WebDriverSimple()
{
System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
this.driver = new FirefoxDriver();
} //启动浏览器
public void startBrowser(String url)
{
driver.get(url);
//最大化浏览器
driver.manage().window().maximize();
} //使用xpath获取元素对象
public WebElement locateElementByXpath(String locator)
{
WebElement element = driver.findElement(By.xpath(locator));
return element;
} //使用CSS获取元素对象
public WebElement locateElementByCss(String locator)
{
WebElement element = driver.findElement(By.cssSelector(locator));
return element;
} /**操作输入框input
* 1.sendKeys代表输入,参数为要输入的值
* 2.clear代表清除输入框中原有的数据
*/
public void testInput(String locator,String content)
{
//WebElement input = this.locateElementByXpath(locator);
WebElement input = this.locateElementByCss(locator);
input.clear();
input.sendKeys(content);
input.clear();
input.sendKeys(content);
} //
/**操作超链接link
* 1.click代表点击这个a链接
*/
public void testLink(String locator)
{
WebElement link = this.locateElementByXpath(locator);
link.click();
} /**操作 下拉框 select
* 1.需要一个Select的类
* 2.selectByValue的参数为option中的value属性
* 3.selectByIndex的参数为option的顺序
* 4.selectByVisibleText的参数为option的text值
*/
public void testSelect(String locator,int index)
{
WebElement element = this.locateElementByXpath(locator);
Select select = new Select(element);
select.selectByIndex(index);
}
public void testSelect(String locator,String value)
{
WebElement element = this.locateElementByXpath(locator);
Select select = new Select(element);
select.selectByValue(value);
//select.selectByVisibleText(text);
} /**操作单选按钮radiobox
* 1.click代表点击选中这个单选框
* 2.isSelected代表检查这个单选框有没有被选中
*/
public void testRaidoBox(String locator)
{
WebElement radio = this.locateElementByXpath(locator);
radio.click();
radio.isSelected();
} /**操作 复选框checkbox
* 1.click代表点击选中这个多选框
* 2.isSelected代表检查这个多选框有没有被选中
*/
public void testCheckBox(String locator)
{
WebElement checkbox = this.locateElementByXpath(locator);
checkbox.click();
checkbox.isSelected();
} /**操作 按钮button
* 1.click代表点击这个按钮
* 2.sEnabled代表检查这个按钮是不是可用的
*/
public void testButton(String locator)
{
WebElement submit = this.locateElementByXpath(locator);
submit.click();
submit.isEnabled();
}
/**操作 上传控件upload
* 1.一般是把路他径直接sendKeys到这个输入框中
* 2.如果输入框被加了readonly属性,不能输入,则需要用JS来去掉readonly属性!
*/
public void testUpload(String locator,String path)
{
WebElement load = this.locateElementByXpath(locator);
load.sendKeys(path);
}
//关闭并退出driver
public void closeBrowser()
{
driver.close();
driver.quit();
}
}
Selenium2(WebDriver)总结(四)---基本元素操作的更多相关文章
- Selenium2(WebDriver)总结(五)---元素操作进阶(常用类)
1.Alert类 Alert是指windows弹窗的一些操作,需要new一个Alert类 driver.switchTo().alert():切换到alert窗口 alert.getText():取得 ...
- selenium2 Webdriver + Java 自动化测试实战和完全教程
selenium2 Webdriver + Java 自动化测试实战和完全教程一.快速开始 博客分类: Selenium-webdriverselenium webdriver 学习selenium ...
- webDriver对element进行操作
非常感谢原作者:eastmount,原地址:http://blog.csdn.net/eastmount/article/details/48108259 感谢感谢 这篇文章主要Seleniu ...
- selenium2 WebDriver 在asp.net项目中的应用
selenium2 WebDriver是一款跨平台的 自动化测试工具,它可以操纵浏览器,模拟用户行为,非常方便用户进行自动化测试. .net项目使用它,首先要通过 Visual Studio 的 nu ...
- selenium2(WebDriver) API
selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/ 1.1 下载selenium2.0的包 官方downl ...
- Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&加载插件
本文主要记录下在使用selenium2/webdriver时启动各种浏览器的方法.以及如何加载插件.定制浏览器信息(设置profile)等 环境搭建可参考我的另一篇文章:http://www.cnbl ...
- AJAX的来龙去脉(由来)-如何被封装出来的--ajax发送异步请求(四步操作)
<黑马程序员_超全面的JavaWeb视频教程vedio\JavaWeb视频教程_day23_ajax> \JavaWeb视频教程_day23_ajax\day23ajax_avi\14.打 ...
- Selenium2(WebDriver)总结(二)---Firefox的firebug插件参数设置(补充)
本文是对上一节的补充:http://www.cnblogs.com/puresoul/p/4251536.html 使用Selenium2(webdriver)启动firefox且自动加载firebu ...
- Git 学习(四)操作修改和版本穿梭
Git 学习(四)操作修改和版本穿梭 之前的章节,已介绍了本地Git库创建.暂存区增.删.改,以及提交版本库:可回顾下命令操作: git add 和 git commit. 光有之前章节的操作,Git ...
随机推荐
- lufylegend:图片的加载和显示
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> <script ...
- Spring Boot开发之流水无情(二)
http://my.oschina.net/u/1027043/blog/406558 上篇散仙写了一个很简单的入门级的Spring Boot的例子,没啥技术含量,不过,其实学任何东西只要找到第一个突 ...
- Nginx 的动态 upstreams
原文地址: http://www.oschina.net/translate/nginx-with-dynamic-upstreams 我最近在工作中做一个设置,我有一个面向用户的 Nginx 服务, ...
- 1300多万条数据30G论坛大数据优化实战经验小结
最近由于某大型网站社区论坛运行效率比较低用户反馈论坛有些卡需要对系统进行优化,论坛性能影响了公司的形象还有网站的流量,当然这也会影响到公司的收入,而且后期还需要长期维护网站的社区论坛服务. 1:并发访 ...
- Convolutional Neural Networks at Constrained Time Cost(精读)
一.文献名字和作者 Convolutional Neural Networks at Constrained Time Cost,CVPR 2015 二.阅读时间 2015年6月30 ...
- Hadoop-2.2.0中文文档—— Common - Native Libraries Guide
概览 这个新手教程描写叙述了native(本地?原生?)hadoop库,包括了一小部分关于native hadoop共享库的讨论. This guide describes the native ha ...
- webrtc fec
转自:http://www.cnblogs.com/webrtc/p/7402570.html WebRTC::FEC [TOC] Tags: WebRTC FEC WebRTC中的 FEC 实现分为 ...
- xheditor-文件上传-java-支持html5-application/octet-stream
package reyo.sdk.utils.file; import java.io.BufferedOutputStream; import java.io.File; import java.i ...
- Process 'command '/Users/lidaqiang/Library/Android/sdk/build-tools/27.0.3/aapt'' finished with non-zero exit value 1
Process 'command '/Users/lidaqiang/Library/Android/sdk/build-tools/27.0.3/aapt'' finished with non-z ...
- Material Designer的低版本兼容实现(六)—— Ripple Layout
新版的Android5.0添加了涟漪效果,虽然开源的库提供了各种控件便于大家使用涟漪效果.但是仍旧不可能满足所有需求,因此我今天改出来一个类叫做,LayoutRipple,其实感觉跟应该叫Ripple ...