selenium webdriver(3)---操作页面对象
页面对象的相关操作可以通过接口文件org.openqa.selenium.WebElement查看,本文只是对象接口的使用方式,具体的实现方式在org.openqa.selenium.remote.RemoteWebElement中,有兴趣的同学可以研究下,接口方法如下图
即将用到的网页
sendKeys
当我们想向输入框中输入内容时采用sendKeys方法
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) { System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("http://shanghai.anjuke.com");
//获得输入框对象
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']")); input.sendKeys("selenium");
}
为了突出重点,后续代码中关于启动driver及打开网址的代码就不再重复写了
click
输入之后的操作就是搜索了,这里有两种方式,第二种稍后介绍:
1.点击输入框旁边的搜索按钮
2.如果输入框在form中,可以之间调用submit方法
//获得输入框对象
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
input.sendKeys("selenium"); //获得搜索按钮对象
WebElement button=driver.findElement(By.xpath("//input[@id='btnSubmit']"));
button.click();
submit
如果当前对象在一个form中的话就可以使用这个方法,相比于click省去了获取button对象这一步,我们安居客的输入框正好在form中,哈哈省事了
//获得输入框对象
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
input.sendKeys("selenium");
input.submit();
clear
clear方法用来清除输入框中的内容
//获得输入框对象
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
input.sendKeys("selenium");
input.clear();
其实使用sendKeys方法可以达到同样的效果,我们想要清除刚才输入的内容"selenium",只要按下8次backspace键即可,请看
//获得输入框对象
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
input.sendKeys("selenium");
for(int i=1;i<=8;i++)
input.sendKeys(Keys.chord(Keys.BACK_SPACE));
ps:clear方法一般用于在sendKeys之前使用,到达初始化的效果
isDisplayed
此方法用来判断对象是否可见,也就是css的display是否为none,本文网页中与input同级的em元素不可见正好可以用来作为例子
//获得input对象
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
if(input.isDisplayed())
System.out.println("input is displayesd");
else
System.out.println("input is not displayesd");
//获得em对象
WebElement em=driver.findElement(By.xpath("//em[@id='home_close']"));
if(em.isDisplayed())
System.out.println("em is displayesd");
else
System.out.println("em is not displayesd");
isEnabled
isEnabled判断对象是否被禁用
//获得input对象
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
if(input.isEnabled())
System.out.println("input is isEnabled");
else
System.out.println("input is not isEnabled");
//获得em对象
WebElement em=driver.findElement(By.xpath("//em[@id='home_close']"));
if(em.isEnabled())
System.out.println("em is isEnabled");
else
System.out.println("em is not isEnabled");
isSelected
该方法用来判断单选框、下拉框、复选框中的选项是否被选中,使用安居客的房贷计算器页面来演示
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("http://www.anjuke.com/calculator/return"); WebElement prePayment=driver.findElement(By.xpath("//input[@id='tqradio']"));
prePayment.click();
if(prePayment.isSelected())
System.out.println("提前还款方式为:部分提前还款");
else
System.out.println("提前还款方式为:一次提前还清");
Thread.sleep(3000);
driver.quit();
}
单选框有很多的操作方法,上面的例子还可以这样写,采用列表的形式
List<WebElement> prePayment=driver.findElements(By.xpath("//input[@name='tqhkfs']"));
prePayment.get(1).click(); if(prePayment.get(1).isSelected())
System.out.println("提前还款方式为:部分提前还款");
else
System.out.println("提前还款方式为:一次提前还清");
getTagName
获取标签名称
WebElement prePayment=driver.findElement(By.xpath("//input[@id='tqradio']")); System.out.println("单选框的标签名称是"+prePayment.getTagName());
getText
获取对象的innerText内容
WebElement methodOfPrePayment=driver.findElement(By.xpath("//div[@class='year']/label")); System.out.println(methodOfPrePayment.getText());
getCssValue
这个方法返回的对象css中定义的属性的值,参数一定要是css支持的属性
WebElement methodOfPrePayment=driver.findElement(By.xpath("//div[@class='year']/label")); System.out.println(methodOfPrePayment.getCssValue("margin-left"));
getAttribute
获取对象属性的值,这个属性指的是html标签的属性如id、class等,注意和getCssValue区别开来
WebElement methodOfPrePayment=driver.findElement(By.xpath("//div[@class='year']/label")); System.out.println(methodOfPrePayment.getAttribute("innerText"));
getLocation
该方法返回对象在页面中的位置(相对于页面左上角)
WebElement methodOfPrePayment=driver.findElement(By.xpath("//div[@class='year']/label")); System.out.println("水平距离"+methodOfPrePayment.getLocation().getX());
System.out.println("垂直距离"+methodOfPrePayment.getLocation().getY());
getSize
获取对象的高和宽
WebElement methodOfPrePayment=driver.findElement(By.xpath("//div[@class='year']/label")); System.out.println("宽"+methodOfPrePayment.getSize().getWidth());
System.out.println("高"+methodOfPrePayment.getSize().getHeight());
getSize、getLocation等方法在UI测试中使用较多,功能测试一般不会用到
下拉框的相关操作
最后在补充一些关于下拉框的操作,下拉框在平时工作中用到的挺多,操作方式也很多,一般的有三种
层级定位
通过层级定位准确的定位到需要操作的下拉选项,操作明确下拉数目、名称的下拉框时比较方便
WebElement methodOfPayment=driver.findElement(By.xpath("//select[@id='tqhklx']")); WebElement methodOfPaymentOption=methodOfPayment.findElement(By.xpath("./option[@value='1']")); methodOfPaymentOption.click(); System.out.println("还款方式为"+methodOfPaymentOption.getText());
列表操作
一次性获取全部下拉选项,在列表中操作
List<WebElement> methodOfPaymentOptions=driver.findElements(By.xpath("//select[@id='tqhklx']/option")); System.out.println("还款方式:"); for(int i=0;i<methodOfPaymentOptions.size();i++)
System.out.println(methodOfPaymentOptions.get(i).getText());
Select类
webdriver提供了一个单独的Select来对下拉框进行操作
Select select=new Select(driver.findElement(By.xpath("//select[@id='tqhklx']"))); List<WebElement> methodOfPaymentOptions=select.getOptions();
System.out.println("还款方式:");
for(int i=0;i<methodOfPaymentOptions.size();i++)
System.out.println(methodOfPaymentOptions.get(i).getText());
Select类提供的方法很多,完全可以满足平时工作的需要,具体的可以到org.openqa.selenium.support.ui.Select中查看
selenium webdriver(3)---操作页面对象的更多相关文章
- JS注入操作页面对象
在用selenium webdriver 编写web页面的自动化测试代码时,有时对页面对象的操作需要通过js语句去执行,selenium本身就支持执行js,我们在代码中import org.openq ...
- python3 + selenium 使用 JS操作页面滚动条
js2 = "window.scrollTo(0,0);" #括号中为坐标 当不知道需要的滚动的坐标大小时: weizhi2 = driver.find_element_by_id ...
- Selenium(Python)页面对象+数据驱动测试框架
整个工程的目录结构: 常用方法类: class SeleniumMethod(object): # 封装Selenium常用方法 def __init__(self, driver): self.dr ...
- selenium常用命令--操作页面元素及获取元素内容整理
selenium常用命令之操作页面元素及获取元素内容的事件整理 例子: /**id <input type="text" id="phone" name ...
- Selenium webdriver Java 操作chrome 浏览器
Step1: 下载chromedriver. 下载路径: http://chromedriver.storage.googleapis.com/index.html 选择一个合适的下载即可.我下载的是 ...
- Selenium webdriver Java 操作IE浏览器
V1.0版本:直接新建WebDriver使用 import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetE ...
- selenium - webdriver - cookie操作
WebDriver提供了操作Cookie的相关方法,可以读取.添加和删除cookie信息. WebDriver操作cookie的方法: get_cookies(): 获得所有cookie信息. get ...
- java selenium webdriver第二讲 页面元素定位
自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选 ...
- selenium webdriver python 操作浏览器
新建driver driver=webdriver.Firefox() driver=webdriver.Ie() driver=webdriver.Chrome() 打开一个链接 driver. ...
随机推荐
- iframe框根据内容自适应高度
1.页面 <iframe name="iframe_userCenter" id="iframe" frameborder=2 width=100% he ...
- [Caffe] ubuntu14.04下使用OpenBLAS加速Caffe
一.apt安装 sudo apt-get install libopenblas-dev 二.手动从source安装 1. 下载OpenBLAS并编译 git clone https://github ...
- Python3 函数式编程
函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量,因此,任意一个函数,只要输入是确定的,输出就是确定的,这种纯函数我们称之为没有副作用.而允许使用变量的程序设计语言,由 ...
- linux环境下安装php扩展
本文只提供源码安装的方法(已安装pcntl为例) 其他方法请参考:http://doc3.workerman.net/appendices/install-extension.html 1.利用php ...
- php学习,一个简单的Calendar(2) 一个简单的活动页面
有了前面的基础,后面就是将页面展示出来. 预览图如下:1号和31号分别有活动,会一并显示出来 这里需要搞定几个问题,一个就是数据库的连接,我们用\sys\class\class.db_connec ...
- JBPM4中常用概念总结
1. 流程定义(Process Definition) 流程定义是记录在xml文件中的对流程的描述,它包含唯一的流程开始节点和多个流程功能节点,每个节点之间使用transition进行连接. P ...
- c#反射机制判断同一个类的两个实例的值是否完全一样
; i < properties1.Length; i++) { string s = properties1[i].DeclaringTyp ...
- jdk .tar.gz 包安装 inAction
方法参考于http://blog.sina.com.cn/s/blog_81631744010137iy.html, 多谢博主分享,此处仅作为个人学习笔记. 首先将*.tar.gz解压 tar -xz ...
- C#中值参数的使用实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 函数的参 ...
- Cloud Insight 现在已经支持监控 Cassandra 啦!
Cassandra 是什么? Apache Cassandra 以其可扩展性和容错分布式数据库系统而被人所熟知.Cassandra 起源于Facebook 最初创建于 Amazon Dynamo 和谷 ...