页面对象的相关操作可以通过接口文件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)---操作页面对象的更多相关文章

  1. JS注入操作页面对象

    在用selenium webdriver 编写web页面的自动化测试代码时,有时对页面对象的操作需要通过js语句去执行,selenium本身就支持执行js,我们在代码中import org.openq ...

  2. python3 + selenium 使用 JS操作页面滚动条

    js2 = "window.scrollTo(0,0);" #括号中为坐标 当不知道需要的滚动的坐标大小时: weizhi2 = driver.find_element_by_id ...

  3. Selenium(Python)页面对象+数据驱动测试框架

    整个工程的目录结构: 常用方法类: class SeleniumMethod(object): # 封装Selenium常用方法 def __init__(self, driver): self.dr ...

  4. selenium常用命令--操作页面元素及获取元素内容整理

    selenium常用命令之操作页面元素及获取元素内容的事件整理 例子:  /**id <input type="text" id="phone" name ...

  5. Selenium webdriver Java 操作chrome 浏览器

    Step1: 下载chromedriver. 下载路径: http://chromedriver.storage.googleapis.com/index.html 选择一个合适的下载即可.我下载的是 ...

  6. Selenium webdriver Java 操作IE浏览器

    V1.0版本:直接新建WebDriver使用 import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetE ...

  7. selenium - webdriver - cookie操作

    WebDriver提供了操作Cookie的相关方法,可以读取.添加和删除cookie信息. WebDriver操作cookie的方法: get_cookies(): 获得所有cookie信息. get ...

  8. java selenium webdriver第二讲 页面元素定位

    自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选 ...

  9. selenium webdriver python 操作浏览器

    新建driver driver=webdriver.Firefox() driver=webdriver.Ie() driver=webdriver.Chrome()   打开一个链接 driver. ...

随机推荐

  1. Glibc和GCC,ARM-LINUX-GCC的关系

    看到有些贴子/blog上提到「Glibc编译器」,这是个错误的用语.Glibc不是编译器,Glibc不是编译器,Glibc不是编译器.重要的事情说三遍.GCC才是编译器.

  2. Codevs 1064 虫食算 2004年NOIP全国联赛提高组

    1064 虫食算 2004年NOIP全国联赛提高组 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 所谓虫食算,就是原先的算式 ...

  3. 新浪微博 iOS SDK获得用户信息

    代码 - (void)getUserInfo { NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2 ...

  4. Java中ArrayList源码分析

    一.简介 ArrayList是一个数组队列,相当于动态数组.每个ArrayList实例都有自己的容量,该容量至少和所存储数据的个数一样大小,在每次添加数据时,它会使用ensureCapacity()保 ...

  5. 解决Mysql的主从数据库没有同步的两种方法

    今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist;   查看下进程是否Sleep太多.发现很正常.show master status; ...

  6. 【转】Oracle job procedure 存储过程定时任务

    原文:Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相 ...

  7. js 的基础知识变量

    什么是变量? 变是一个存储和释放我的数据! 我们用var关键字来声名变量,声名多个变量时用逗号来隔开 在变量没有赋值之前,显示是一个未定义的变量! <script> var a; var ...

  8. 服务器慢 mysql-bin.000001文件占满磁盘的原因与解决

    发现 VPS 服务器上的网站反应超级慢,简单的重启.重启各主要服务,发现mysql 的反应极其不正常. 一方面是问题,这与站点访问量有关.开始时从mysql 的配置文件 my.cnf 考虑,但志文工作 ...

  9. JavaScript高级之闭包的概念及其应用

    主要内容: 什么是闭包 闭包使用的一般模式 闭包都能做些什么 本文是我的JavaScript高级这个系列中的第二篇文章. 在这个系列中,我计划分析说明 一下JavaScript中的一些常用的而又神秘的 ...

  10. 用19种编程语言写Hello World

    用19种编程语言写Hello World 转载自:http://www.admin10000.com/document/394.html Hello World 程序是每一种编程语言最基本的程序,通常 ...