输入框:input

 表现形式:
     1.在html中一般为:<input id="user" type="text">
主要操作:
     1.driver.findElement(By.id("user")).sendKeys("test");
     2.driver.findElement(By.id("user")).clear()
说明:
    1.sendKeys代表输入,参数为要输入的值
    2.clear代表清除输入框中原有的数据

超链接:a

表现形式:
     1.在html中一般为: <a class="baidu" href="http://www.baidu.com">baidu</a>
主要操作:
     1.driver.findElement(By.xpath("//div[@id='link']/a")).click();
说明:
    1.click代表点击这个a链接

下拉菜单:select

表现形式:

1.在HTML中一般为:
    <select name="select">
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
           <option value="opel">Opel</option>
           <option value="audi">Audi</option>
    </select>

主要操作:
    WebElement element = driver.findElement(By.cssSelector("select[name='select']"));
    Select select = new Select(element);
    select.selectByValue("opel");
    select.selectByIndex(2);
    select.selectByVisibleText("Opel");
说明:
   1.需要一个Select的类
   2.selectByValue的参数为option中的value属性
   3.selectByIndex的参数为option的顺序
   4.selectByVisibleText的参数为option的text值

单选:radiobox

表现形式:
     1.在HTML中一般为:<input class="Volvo" type="radio" name="identity">
主要操作:
     List<WebElement> elements = driver.findElements(By.name("identity"));
     elements.get(2).click();
     boolean select = elements.get(2).isSelected();
说明:
    1.click代表点击选中这个单选框
    2.isSelected代表检查这个单选框有没有被选中

多选:checkbox

表现形式:
     1.在HTML中一般为:<input type="checkbox" name="checkbox1">
主要操作:
     List<WebElement> elements = driver.findElements(By.xpath("//div[@id='checkbox']/input"));
     WebElement element = elements.get(2);
     element.click();
     boolean check = element.isSelected();
说明:
    1.click代表点击选中这个多选框
    2.isSelected代表检查这个多选框有没有被选中

按钮:button

表现形式:
    1.在HTML中,一般有两种表现形式:
      <input class="button" type="button" disabled="disabled" value="Submit">
      <button class="button" disabled="disabled" > Submit</button>
主要操作:
     WebElement element = driver.findElement(By.className("button"));
     element.click();
     boolean button = element.isEnabled();
说明:
    1.click代表点击这个按钮
    2.isEnabled代表检查这个按钮是不是可用的

Alert 类介绍

表现形式:
      1.Alert 类,是指windows弹窗的一些操作
主要操作:
     driver.findElement(By.className("alert")).click();
     Alert alert = driver.switchTo().alert();
     String text = alert.getText();
     alert.accept();
 说明:
      1.先要switch到windows弹窗上面
      2.该Alert类有二个重要的操作getTest(),取得弹窗上面的字符串,accept是指点击确定/ok类的按钮,使弹窗消失

Action类介绍

表现形式:
      1.一般是在要触发一些js函数或者一些JS事件时,要用到这个类
      2.<input class="over" type="button" onmouseout="mouseOut()" onmouseover="mouseOver()" value="Action">
主要操作:
     WebElement element = driver.findElement(By.className("over"));
     Actions action = new Actions(driver);
     action.moveToElement(element).perform();
说明:
     1.先要new一个Actions的类对象
     2.主要操作大家可以自已去看下API,但是最后的perform()一定要加上,否则执行不成功,切记!

上传文件操作

 表现形式:
       1.在html中表现为:<input id="load" type="file">
 说明:
       1.一般是把路他径直接sendKeys到这个输入框中
       2.如果输入框被加了readonly属性,不能输入,则需要用JS来去掉readonly属性!

调用JS介绍

 目的:
       1.执行一段JS,来改变HTML
       2.一些非标准控件无法用selenium2的API时,可以执行JS的办法来取代
主要操作:
      JavascriptExecutor j = (JavascriptExecutor)driver;
      j.executeScript("alert('hellow rold!')");
 说明:
      1.executeScript这个方法的参数为字符串,为一段JS代码
      2.注意,JS代码需要自已根本项目的需求来编写!

Iframe操作

表现形式:

1.在HTML中为:<iframe width="800" height="330" frameborder="0" src="./demo1.html" name="aa">

主要操作:
   driver.switchTo().frame("aa");
说明:
   1.如果iframe标签有能够唯一确定的id或者name,就可以直接用id或者name的值:driver.switchTo().frame("aa");
   2.如果iframe标签没有id或者name,但是我能够通过页面上确定其是第几个(也就是通过index来定位iframe,index是从0开始的):driver.switchTo().frame(0);
   3.也可以通过xpath的方式来定位iframe,写法如下:
      ①WebElement iframe = driver.findElement(By.xpath("//iframe[@name='aa']"));
      ②driver.switchTo().frame(iframe);

多窗口切换

表现形式:
      1.在HTML中一般为:<a class="open" target="_bank" href="http://baidu.com">Open new window</a>
主要操作:
      Set<String> handles = driver.getWindowHandles();
      driver.switchTo().window()
说明:
      1.getWindowHandles是取得driver所打开的所有的页面的句柄;
      2.switchTo是指切换到相应的窗口中去,window中的参数是指要切过去的窗口的句柄;

Wait 机制及实现

目的:

1.让元素对象在可见后进行操作, 取代Thread.sleep, 使其变成智能等待
主要操作:
   boolean wait = new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
   public Boolean apply(WebDriver d) {
   return d.findElement(By.className("red")).isDisplayed();
   } });
说明:
   1.在规定的时间内只要符合条件即返回,上面的代码中是只要isDisplayed即返回;
   2.应用到了WebDriverWait类,这种写法,请大家务必熟练;

具体代码如下:

package com.browser.test;

import java.util.List;
import java.util.Set; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait; public class BasicControl {
public static WebDriver Driver; /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
startFireFox("file:///D:/BaiduYunDownload/selenium2/demo.html");
testWait();
// testMultiWindows();
// testIframe();
// testjs();
// testUpload();
// testAction();
// testAlert();
// testButton();
// testCheckBox();
// testRadioBox();
// testSelect();
// testLink();
} public static void testWait() throws InterruptedException {
WebElement element = Driver.findElement(By.className("wait"));
element.click();
// Thread.sleep(6000);
boolean wait = new WebDriverWait(Driver, 10)
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.className("red")).isDisplayed();
}
});
element = Driver.findElement(By.className("red"));
System.out.println(element.getText());
} public static void testMultiWindows() throws InterruptedException {
WebElement element = Driver.findElement(By.className("open"));
element.click();
Set<String> handles = Driver.getWindowHandles();// get all handles
String handle = Driver.getWindowHandle();// current window handle
handles.remove(handle);// remove the first handle(current window handle) Driver.switchTo().window(handles.iterator().next());
Thread.sleep(2000);
Driver.findElement(By.id("kw")).sendKeys("mystring");
Thread.sleep(2000);
Driver.close();
// Driver.quit();// all webDriver will be closed. Driver.switchTo().window(handle);
Thread.sleep(2000);
Driver.findElement(By.id("user")).sendKeys("new test");
} public static void testIframe() throws InterruptedException {
Driver.findElement(By.id("user")).sendKeys("test");
WebElement element = Driver.findElement(By
.xpath("//iframe[@name='aa']"));
Driver.switchTo().frame(element);
// Driver.switchTo().frame("aa");
// Driver.switchTo().frame(0);
Driver.findElement(By.id("user")).sendKeys("ifreame test");
Thread.sleep(1000);
Driver.switchTo().defaultContent();
Driver.findElement(By.id("user")).sendKeys("new test");
} public static void testjs() {
Driver.get("http://www.haosou.com/");
String ret = (String) ((JavascriptExecutor) Driver)
.executeScript("return document.getElementById('search-button').value;");
System.out.println(ret); JavascriptExecutor j = (JavascriptExecutor) Driver;
j.executeScript("alert('hellow rold!')");
} public static void testUpload() {
WebElement element = Driver.findElement(By.id("load"));
element.sendKeys("D:\\BaiduYunDownload\\selenium2\\第五周 selenium2常用类介绍\\第五周");
} public static void testAction() {
WebElement element = Driver.findElement(By.className("over"));
Actions action = new Actions(Driver);
action.moveToElement(element).perform();
System.out.println(Driver.findElement(By.id("over")).getText());
// action.click(element).perform();
// System.out.println(Driver.findElement(By.id("over")).getText());
} public static void testAlert() {
Driver.findElement(By.className("alert")).click();
Alert alert = Driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept(); WebElement element = Driver.findElement(By.className("alert"));
Actions action = new Actions(Driver);
action.click(element).perform();
Alert alert1 = Driver.switchTo().alert();
System.out.println(alert1.getText());
alert1.accept();
} public static void testButton() {
WebElement element = Driver.findElement(By
.xpath("//div[@id='button']/input"));
System.out.println("The button is enabled:" + element.isEnabled());
element.click();
System.out.println("The button is selected:" + element.isSelected());
} public static void testCheckBox() {
List<WebElement> elements = Driver.findElements(By
.xpath("//div[@id='checkbox']/input"));
System.out.println("The second checkbox is selected:"
+ elements.get(2).isSelected());
elements.get(2).click();
System.out.println("The second checkbox is selected:"
+ elements.get(2).isSelected());
for (int i = 0; i < elements.size(); i++) {
if (elements.get(i).isSelected() == false) {
elements.get(i).click();
}
}
} public static void testRadioBox() {
List<WebElement> elements = Driver.findElements(By.name("identity"));
elements.get(2).click();
System.out.println(elements.get(2).isSelected()); } public static void testSelect() {
Select select = new Select(Driver.findElement(By
.xpath("//select[@name='select']")));
select.selectByValue("audi");
select.selectByIndex(2);
select.selectByVisibleText("Audi");
} public static void testLink() {
Driver.findElement(By.xpath("//div[@id='link']/a")).click();
} public static void testInput(String inputText) {
Driver.findElement(By.id("user")).sendKeys(inputText);
Driver.findElement(By.id("user")).clear();
} public static void startFireFox(String url) {
Driver = new FirefoxDriver();
Driver.manage().window().maximize();
Driver.navigate().to(url);
} public static void closeFireFox() {
Driver.close();
Driver.quit();
}
}

最后打个广告,不要介意哦~

最近我在Dataguru学了《软件自动化测试Selenium2》网络课程,挺不错的,你可以来看看!要是想报名,可以用我的优惠码 G863,立减你50%的固定学费!

链接:http://www.dataguru.cn/invite.php?invitecode=G863

selenium2基本控件介绍及其代码的更多相关文章

  1. 基于CkEditor实现.net在线开发之路(3)常用From表单控件介绍与说明

    上一章已经简单介绍了CKEditor控件可以编写C#代码,然后可以通过ajax去调用,但是要在网页上面编写所有C#后台逻辑,肯定痛苦死了,不说实现复杂的逻辑,就算实现一个简单增删改查,都会让人头痛欲裂 ...

  2. iOS开发UI篇—UIScrollView控件介绍

    iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...

  3. WPF Step By Step 控件介绍

    WPF Step By Step 控件介绍 回顾 上一篇,我们主要讨论了WPF的几个重点的基本知识的介绍,本篇,我们将会简单的介绍几个基本控件的简单用法,本文会举几个项目中的具体的例子,结合这些 例子 ...

  4. ASP.NET服务端基本控件介绍

    ASP.NET服务端基本控件介绍 大概分为三种控件: HTML控件,ASP.NET把HTML控件当成普通字符串渲染到浏览器端,不去检查正确性,无法在服务端进行处理ASP.NET服务端控件,经过ASP. ...

  5. Android support library支持包常用控件介绍(二)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...

  6. R-----shiny包的部分解释和控件介绍

    R-----shiny包的部分解释和控件介绍 作者:周彦通.贾慧 shinyApp( ui = fixedPage( fixedPanel( top = 50, right=50, width=200 ...

  7. Android 一个日历控件的实现代码

    转载  2017-05-19   作者:Othershe   我要评论 本篇文章主要介绍了Android 一个日历控件的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看 ...

  8. CPF 入门教程 - 各个控件介绍(八)

    CPF C#跨平台桌面UI框架 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) CPF 入门教程 - 样式和动画(三) CPF 入门教程 - 绘图(四) CPF 入门 ...

  9. android xml 常用控件介绍

    android常用控件介绍 ------文本框(TextView)     ------列表(ListView)     ------提示(Toast)     ------编辑框(EditText) ...

随机推荐

  1. 移动端之js控制rem,适配字体

    方法一:设置fontsize 按照iphone 5的适配  1em=10px    适配320 // “()()”表示自执行函数 (function (doc, win) { var docEl = ...

  2. vue2.0:(八-2)、外卖App弹窗部分sticky footer

    什么是sticky-footer ? 如果页面内容不够长的时候,页脚块粘贴在视窗底部,如果内容足够长时,页脚块会被内容向下推送.那具体要怎么做呢?下面以外卖App为例: 第一种方法:这个自己用过,是好 ...

  3. webstorm增加内存配置参数

    webstorm增加内存配置参数 找到WebStorm.exe.vmoptions这个文件,路径如下 webstorm安装主目录>bin>WebStorm.exe.vmoptions 更改 ...

  4. Web前端体系的脉络结构

    Web前端技术由 html.css 和 javascript 三大部分构成,是一个庞大而复杂的技术体系,其复杂程度不低于任何一门后端语言.而我们在学习它的时候往往是先从某一个点切入,然后不断地接触和学 ...

  5. LoadRunner使用(1)

    一.LoadRunner脚本录制 LoadRunner测试分为两个步骤: 第一步:录制脚本,其实就是监控并记录这段时间发送的HTTP请求 第二步:启动多个线程,用录制的脚本,模拟多线程发送请求. (1 ...

  6. UVA 12901 Refraction 折射 (物理)

    一道物理题,解个2次方程就行了... 求h最小的情况对应如下图所示 做法不唯一,我想避免精度损失所以在化简的时候尽可能地去避免sqrt和浮点数乘除. 似乎精度要求很低,直接用角度算也可以 #inclu ...

  7. SG函数入门&&HDU 1848

    SG函数 sg[i]为0表示i节点先手必败. 首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数.例如mex{0,1,2,4}=3. ...

  8. String java

    https://www.golinuxcloud.com/java-interview-questions-answers-experienced-2/ 75. What is the meaning ...

  9. 2019.05.26 周日--《阿里巴巴 Java 开发手册》精华摘要

    一.写在开头 Java作为一个编程界最流行的语言之一,有着很强的生命力.代码的编写规范也是不容忽视的,今天,我就把自己阅读的国内的互联网巨头阿里巴巴的<阿里巴巴 Java 开发手册>一些精 ...

  10. mysql 安装简介

    Linux: 安装 [root @ localhost ~]# yum install mysql-server 设定为开机自动启动 [root @ localhost ~]# chkconfig m ...