1.1  下载selenium2.0的包

  1. 官方download包地址:http://code.google.com/p/selenium/downloads/list
  2. 官方User Guide:   http://seleniumhq.org/docs/
  3. 官方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浏览器

    WebDriverdriver = new HtmlUnitDriver();

  • 打开chrome浏览器

       WebDriverdriver = 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 简单指南的更多相关文章

  1. Selenium 简单的例子

    Selenium是一个web自动化验收测试框架.   Selenium Client Driver - Selenium 2.0 Document http://seleniumhq.github.i ...

  2. python+selenium 简单尝试

    前言 selenium是一种自动化测试工具,简单来说浏览器会根据写好的测试脚本自动做一些操作. 关于自动化测试,一开始接触的是splinter,但是安装的时候发现它是基于selenium的,于是打算直 ...

  3. 自动化测试基础篇--Selenium简单的163邮箱登录实例

    摘自https://www.cnblogs.com/sanzangTst/p/7472556.html 前面几篇内容一直讲解Selenium Python的基本使用方法.学习了什么是selenium: ...

  4. Selenium简单回顾

    一.Selenium介绍 1.Selenium(浏览器自动化测试框架): Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的 ...

  5. 在Github和Git上fork之简单指南

    http://www.linuxidc.com/Linux/2014-11/109785.htm 以我的经验来看,刚接触Git和GitHub时,最困扰的一件事情就是尝试解决下面的问题:在Git和Git ...

  6. Selenium简单测试页面加载速度的性能(Page loading performance)

    利用selenium的可以执行javascript脚本的特性,我写了一个java版本的获得页面加载速度的代码,这样你就可以在进行功能测试的同时进行一个简单的测试页面的加载速度的性能测试. 我现在的项目 ...

  7. selenium简单使用

    简介 Selenium是一个用于Web应用程序测试的工具.Selenium可以直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Fi ...

  8. python中通过selenium简单操作及xpath元素定位&轴定位

    浏览器的简单操作 # 导入webdriver模块 # 创建driver对象,指定Chrome浏览器 driver = webdriver.Chrome() # 窗口最大化 driver.maximiz ...

  9. python自动化测试工具selenium使用指南

    概述 selenium是网页应用中最流行的自动化测试工具,可以用来做自动化测试或者浏览器爬虫等.官网地址为:https://www.selenium.dev/.相对于另外一款web自动化测试工具QTP ...

随机推荐

  1. Django 组件content_type

    content type: django内置组件,这个组件帮忙做连表操作(混搭连表) 适用场景:适用于一张表与多张表同时做关联的时候.直接导入就可以使用了. 关联数据库所有的表:可以快速插入数据,并且 ...

  2. 马士兵Spring-声明式事务管理-annotation

    1.事务加在DAO层还是service层? service中可能多涉及多种DAO的操作,比如存了一个User之后,需要保存一条日志信息:如果在DAO中分别设置事务的话,一个DAO下面方法抛出异常了,但 ...

  3. codeforce1029B B. Creating the Contest(简单dp,简单版单调栈)

    B. Creating the Contest time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. ESN

    1.对于一般的硬件设备,ESN是设备序列号,主要用来识别设备,包括未来服务鉴权的需要 2.对于需要license的设备,ESN也是设备序列号的意思,只不过这个序列号可能是根据设备硬件信息算出来的一串字 ...

  5. C++实现大正整数及其相关运算(长期更新)

    /** 只考虑正数[1, +∞); “-”运算只允许大数减小数; 小端存储: */ typedef struct BigInteger0 { vector<int> v; BigInteg ...

  6. bootstrap插件的一些常用属性介绍

    1.下拉菜单 <div class="dropdown"> <button class="btn btn-default dropdown-toggle ...

  7. Python中常见的数据类型总结

    Python提供多种数据类型来存放数据项集合,主要包括序列(列表list和元组tuple),映射(如字典dict),集合(set),下面对这几种一一介绍: 一 序列 1.列表list 列表是一种有序的 ...

  8. J.U.C 整体认识

    深入浅出 Java Concurrency (1) : J.U.C的整体认识 去年年底有一个Guice的研究计划,可惜由于工作“繁忙”加上实际工作中没有用上导致“无疾而终”,最终只是完成了Guice的 ...

  9. http协议再复习(二)

    HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法. HTTPS(Hypertext Transfer ...

  10. Air21

    handler package com.icil.edi.ws.milestoneService.handler; import java.text.SimpleDateFormat; import ...