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. ...
随机推荐
- java新手笔记17 参数
package com.yfs.javase; public class ParamDemo { public static void main(String[] args) { int a = 3, ...
- 【转】C#窗体飞入飞出的动画效果(Api)
[System.Runtime.InteropServices.DllImport("user32")] private static extern bool AnimateWin ...
- php验证复选框有效性的示例
本文介绍一个简单的php通过代码验证复选框值的有效性,有需要的可以参考一下 验证复选框的php代码,如下: 复制代码代码如下: <?php /** * 在php中验证复选框的有效性 * ...
- An easy way to syncTime using C#
/* * Created by SharpDevelop. * User: Administrator * Date: 2013/10/23 * Time: 8:57 * author zibet * ...
- 解决android调用IIS Express中的WCF服务时,出现错误400问题
IIS Express仅支持localhost主机名地址访问. 找到IIS Express Config文件下的 applicationhost.confi 修改配置 再来调试android应用, ...
- 在使用MOS管时要注意的问题
1.当Vds电压增大,Ciss增大,栅极充放电电流也会增大 2.MOS管的功率损耗要控制在额定功耗以下 3.在Buck电路中,开关MOS管的Vds在MOS管关断时会非常大
- 怎么利用ultraISO对一个文件夹制作ISO镜像
1. 运行UltraISO软件,选择好需要转换成ISO的文件,直接拖到UltraISO的界面中. 2.点击“另存为”按钮,选择存放路径并写上ISO文件的名称,文件保存类型选择为ISO格式,点击“保存” ...
- Prism vs MvvmCross
Prism vs MvvmCross 在上一篇Xamarin开发环境及开发框架初探中,曾简单提到MvvmCross这个Xamarin下的开发框架.最近又评估了一些别的,发现老牌Mvvm框架Prism现 ...
- sublime部署开发环境
安装nodejs 去官网下载安装,暂时不建议安装5.0以上版本. 命令行运行以下命令查看是否安装成功: 全局安装gulp $ npm install -g gulp 全局安装requirejs 压缩资 ...
- Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights
一个简单的题: 感觉像计算几何,其实并用不到什么计算几何的知识: 方法: 首先对每条边判断一下,看他们能够把平面分成多少份: 然后用边来对点划分集合,首先初始化为一个集合: 最后如果点的集合等于平面的 ...