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. 写代码注意了,打死都不要用 User 这个单词

    阅读本文大概需要 4 分钟. 原文:http://t.cn/Eau2d0h 译文:http://21cto.com/article/2093 当你意识到你在项目开始时做的轻量.简单的设想竟然完全错了时 ...

  2. java基础 类 & 继承

    类 在Java中,类文件是以.java为后缀的代码文件,在每个类文件中可以有多个类,但是最多只允许出现一个public类,当有public类的时候,类文件的名称必须和public类的名称相同,若不存在 ...

  3. GPG实践

    遇见的问题 安装之后没有显示如教程中的 直接提示真实姓名于电子邮件的地址 公钥与密钥 设置吊销证书

  4. cad.net IExtensionApplication接口的妙用 分开写"启动运行"函数

    cad提供的 IExtensionApplication 接口 是不能实现一次以上的,那么这给编写代码带来了一种不好的情况是,每次都要去修改实现这个接口的类, 如果是一个小的测试功能,你又要去动前面的 ...

  5. How to let your website login with domain account when using IIS to deploy it?

    如何让你的网站以域账号登录 Select your website in IIS Manager, open Authentication, enable Windows Authentication ...

  6. Github 上优秀的 Java 项目推荐

    1.JavaGuide 地址:Snailclimb/JavaGuide [Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. 2.DoraemonKit 地址:didi/Do ...

  7. 卸载webpack,降低版本

    卸载:npm uninstall webpack -g 重新安装:npm install webpack@3.7.1 -g

  8. jmeter(二十七)分布式压测注意事项

    之前的博客:jemter(二十三):分布式测试简略的介绍了利用jmeter做分布式测试的方法,当时只是介绍了背景和原因,以及基本的配置操作,有同学说写得不够详细. 正好今年双十一,我司的全链路压测,也 ...

  9. Entity Framework Core今日所得:避免 IEnumerable 以及 IQueryable 陷阱

    避免 IEnumerable 以及 IQueryable 陷阱: IEnumerable示用Linq会先去数据库查询所有记录,然后再条件查询. IQueryable接口派生自IEnumerable,但 ...

  10. Jmeter websocket插件安装与使用

    Jmeter websocket插件安装与使用 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试 ...