selenium 简单指南
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();
- 打开IE浏览器
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对象,其封装了与导航相关的一些方法,比如前进后退等
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);
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); //异步脚本的超时时间
selenium 简单指南的更多相关文章
- Selenium 简单的例子
Selenium是一个web自动化验收测试框架. Selenium Client Driver - Selenium 2.0 Document http://seleniumhq.github.i ...
- python+selenium 简单尝试
前言 selenium是一种自动化测试工具,简单来说浏览器会根据写好的测试脚本自动做一些操作. 关于自动化测试,一开始接触的是splinter,但是安装的时候发现它是基于selenium的,于是打算直 ...
- 自动化测试基础篇--Selenium简单的163邮箱登录实例
摘自https://www.cnblogs.com/sanzangTst/p/7472556.html 前面几篇内容一直讲解Selenium Python的基本使用方法.学习了什么是selenium: ...
- Selenium简单回顾
一.Selenium介绍 1.Selenium(浏览器自动化测试框架): Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的 ...
- 在Github和Git上fork之简单指南
http://www.linuxidc.com/Linux/2014-11/109785.htm 以我的经验来看,刚接触Git和GitHub时,最困扰的一件事情就是尝试解决下面的问题:在Git和Git ...
- Selenium简单测试页面加载速度的性能(Page loading performance)
利用selenium的可以执行javascript脚本的特性,我写了一个java版本的获得页面加载速度的代码,这样你就可以在进行功能测试的同时进行一个简单的测试页面的加载速度的性能测试. 我现在的项目 ...
- selenium简单使用
简介 Selenium是一个用于Web应用程序测试的工具.Selenium可以直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Fi ...
- python中通过selenium简单操作及xpath元素定位&轴定位
浏览器的简单操作 # 导入webdriver模块 # 创建driver对象,指定Chrome浏览器 driver = webdriver.Chrome() # 窗口最大化 driver.maximiz ...
- python自动化测试工具selenium使用指南
概述 selenium是网页应用中最流行的自动化测试工具,可以用来做自动化测试或者浏览器爬虫等.官网地址为:https://www.selenium.dev/.相对于另外一款web自动化测试工具QTP ...
随机推荐
- ThinkJava-压缩
尽管存在许多种压缩算恙,但是Zip和GZIP可能是最常用的.因此我们可以很容易地使用多 种可读写这些格式的工具来操纵我们的压缩数据. 1 用GZIP进行简单压缩 GZIP接口非常简单, 因此如果我 ...
- InnoDB引擎,从大表中删除多行
官方建议: InnoDB Tables If you are deleting many rows from a large table, you may exceed the lock table ...
- 模板引擎文档 - layui.laytpl 介绍
<!DOCTYPE html> <html class="ui-page-login"> <head> <meta charset=&qu ...
- Python 2.75升级3.6.3
https://blog.csdn.net/wwwdaan5com/article/details/78218277 Centos 7 默认yum安装python 是2.7.5, (网上看了很多升级都 ...
- login oracle as sysdba
- 【332】Machine Learning
Reference: 决策树方法-对买电脑进行分类预测 Reference: 最邻近规则分类(K-Nearest Neighbor)KNN算法应用 Reference: python 内建函数 str ...
- ansible debug
<demoredis_redis_1> ESTABLISH DOCKER CONNECTION FOR USER: ?<demoredis_redis_1> EXEC ['/u ...
- Thrift 实现 JAVA,PHP,C#,Python 互相通信
Thrift介绍 https://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/index.html 首先需要下载 Thrift.exe ...
- js输出/获得Cookie
js输出/获得Cookie //方法 1 function setCookie(name, value) { var Days = 365; var exp = new Date(); exp.set ...
- TokuDB的索引结构–分形树的实现
分形树简介 原文:http://www.bitstech.net/2015/12/15/tokudb-index-introduction/ 分形树是一种写优化的磁盘索引数据结构. 在一般情况下, 分 ...