selenium:
selenium2(WebDriver) API
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浏览器

    WebDriver driver = new HtmlUnitDriver();

打开chrome浏览器

       WebDriver driver = 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 api学习的更多相关文章

  1. Selenium webdriver 学习总结-元素定位

    Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...

  2. Python学习--Selenium模块学习(2)

    Selenium的基本操作 获取浏览器驱动寻找方式 1. 通过手动指定浏览器驱动路径2. 通过 `$PATH`环境变量找寻浏览器驱动 可参考Python学习--Selenium模块简单介绍(1) 控制 ...

  3. 自动化测试 python2.7 与 selenium 2 学习

    windows环境搭建 # 下载 python[python 开发环境] http://python.org/getit/ # 下载 setuptools [python 的基础包工具]setupto ...

  4. Selenium API 介绍

    Selenium API 介绍 我们先前学习过元素定位,大家不知道学习得怎么样了,当你学会元素定位之后就能够跟着我的脚步学习本节Selenium 经常使用的API 介绍 Seleium 为什么能模拟人 ...

  5. Openstack api 学习文档 & restclient使用文档

    Openstack api 学习文档 & restclient使用文档 转载请注明http://www.cnblogs.com/juandx/p/4943409.html 这篇文档总结一下我初 ...

  6. ASP.NET MVC Web API 学习笔记---第一个Web API程序

    http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...

  7. Selenium2+python自动化27-查看selenium API

    前言 前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就叫大家如何去查看selenium api,不求人,无需伸手找人要,在自己电脑就有. p ...

  8. Selenium Grid 学习笔记

    Selenium Grid 学习笔记http://www.docin.com/p-765680298.html

  9. Robot Framework自动化测试(三)---Selenium API

    Robot  Framework  Selenium  API 说明: 此文档只是将最常用的UI 操作列出.更多方法请查找selenium 关键字库. 一.浏览器驱动 通过不同的浏览器执行脚本. Op ...

随机推荐

  1. windows 中如何定位恶意软件的藏身位置

    目录 一: 下载spy++ 打开后 点击 搜索下面的查找窗口(Alt+F3) 点击 查找程序工具 右侧的 靶子一样的图标,鼠标左键按住不放,拖放到 弹窗上面,弹窗周围会出现 黑框. 然后 我们点击确定 ...

  2. 热点Key问题的发现与解决

    热点问题概述 产生原因 热点问题产生的原因大致有以下两种: 用户消费的数据远大于生产的数据(热卖商品.热点新闻.热点评论.明星直播). 在日常工作生活中一些突发的的事件,例如:双十一期间某些热门商品的 ...

  3. concurrent (一)concurrent

    参考文档: 跳跃表原理分析:https://blog.csdn.net/a1259109679/article/details/46442895 一.阻塞队列 ArrayBlockingQueue : ...

  4. Golang(四)正则表达式使用

    0. 前言 最近用到了 regexp 包,下面整理下正则表达式相关用法 参考 基础知识 - Golang 中的正则表达式 和 Golang regexp包中的函数和方法 做了汇总 1. 正则表达式 1 ...

  5. VS2019 NetCore3.0找寻grpc模板

    今天研究Google的grpc框架的时候看到了https://www.cnblogs.com/yilezhu/p/10631420.html这哥们儿的博客 按照博客的内容找寻grpc模板,始终找不到A ...

  6. ASP.NET Core使用Docker进行容器化托管和部署

    一.课程介绍 人生苦短,我用.NET Core!今天给大家分享一下Asp.Net Core以Docker进行容器化部署托管,本课程并不是完完全全的零基础Docker入门教学,课程知识点难免有没覆盖全面 ...

  7. centos7 安装hadoop2.7.6(分布式)

    本文只做简单介绍,具体步骤操作请参考centos6.5 安装hadoop1.2.1亲测版 本篇只简单介绍安装步骤 1.安装目录 /usr/local/hadoop (HADOOP_HOME) 2,创建 ...

  8. php扩展模块 opcache安装教程

    php扩展模块 opcache安装教程PHP5.5.0以后版本自带Opcache加速器,但默认情况下木有启用.所以编译PHP的时候 我们想要启用该PHP加速器就应该添加参数 : –enable-opc ...

  9. C语言字节对齐分析

    1.前言 什么是字节对齐呢?现代计算机中的内存空间都是按字节(byte)划分的,从理论上讲似乎任何类型的变量的访问都可以从任何地址开始,但是实际情况是在访问特定变量的时候经常需要在特定的内存地址进行访 ...

  10. linux下杀掉某用户所有进程

    直接删除用户,提示该用户下还有进程,以下两种方法可解决: 1.结束所有username的进程(如果提示没有该命令,那么用下面方法) killall -u username 2.杀死某一用户下的所有进程 ...