(转载)

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();

        如果firefox不是默认安装路径,需要指定它的路径再启动:

        System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");

        WebDriver webDriver = new FirefoxDriver();

  • 打开IE浏览器

        WebDriver driver = new InternetExplorerDriver ();

        也可以指定准确的IE路径,然后启动它:

        System.setProperty("webdriver.ie.driver",
                    "C:/Program Files (x86)/Internet Explorer/iexplore.exe");
              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();
Driver.Quit() Quit this dirver, closing every associated windows;
Driver.Close() Close the current window, quiting the browser if it is the last window currently open.

1.3  打开测试页面

  • driver.get("http://www.google.com");
  • driver.navigate().to("http://www.baidu.com/");

      P.S.navigate方法会产生1个Navigator对象,其封装了与导航相关的一些方法,比如前进后退等

      navigate()进行页面跳转时不会进行页面数据的校验(比如当前页面有必填项但是实际上没填,navigate也会跳到to页面),相反get()则可以进行这些校验,更真实的模拟实际情况

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);

小技巧:当使用某一属性识别元素时如果返回了多个,可通过先找到唯一的父元素,然后再找他的子元素,如下,

WebElement web=driver.findElement(By.id("form")).findElement(By.id("kw"));

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);  //异步脚本的超时时间

Webdriver API (一)的更多相关文章

  1. 转:python webdriver API 之操作测试对象

    一般来说,所有有趣的操作与页面交互都将通过 WebElement 接口,包括上一节中介绍的对象定位,以及本节中需要介绍的常对象操作.webdriver 中比较常用的操作元素的方法有下面几个: cle ...

  2. Webdriver API (二)

    (转载) 1.3 打开测试页面 对页面对测试,首先要打开被测试页面的地址(如:http://www.google.com),web driver 提供的get方法可以打开一个页面: // And no ...

  3. selenium2(WebDriver) API

    selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/  1.1  下载selenium2.0的包 官方downl ...

  4. Selenium2+Python:Webdriver API速记手册

    由于web自动化常常需要控制浏览器行为和操作页面元素,相关函数又比较多,于是再此记下一份Webdriver API查阅文档以备不时之需. 参考:虫师<Selenium2自动化测试实战>,和 ...

  5. webdriver API中文文档

    1.1   下载selenium2.0的lib包 http://code.google.com/p/selenium/downloads/list 官方UserGuide:http://seleniu ...

  6. python+selenium自动化软件测试(第2章):WebDriver API

    2.1 操作元素基本方法 前言前面已经把环境搭建好了,从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可 ...

  7. Selenium WebDriver Api 知识梳理

    之前一直没有系统的梳理WebDriver Api的相关知识,今天借此机会整理一下. 1.页面元素定位 1.1.8种常用定位方法 # id定位 driver.find_element_by_id() # ...

  8. 2.28 查看webdriver API

    2.28 查看webdriver API(带翻译) 前言    前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就教大家如何去查看seleni ...

  9. python2.7运行selenium webdriver api报错Unable to find a matching set of capabilities

    在火狐浏览器33版本,python2.7运行selenium webdriver api报错:SessionNotCreatedException: Message: Unable to find a ...

  10. Webdriver API中文版

    Webdriver API中文版 1.1   下载selenium2.0的lib包 http://code.google.com/p/selenium/downloads/list 官方UserGui ...

随机推荐

  1. mybatis随意sql语句

    mybatis的mapper.xml随意sql语句, 不管表之间存不存在关系, 都可以使用, 但注意resultMap中一定要指定查询数据返回的列 或 对象(其实就是多列封装到一个对象中) <? ...

  2. python总字符串

    前面我们讲解了什么是字符串.字符串可以用''或者""括起来表示. 如果字符串本身包含'怎么办?比如我们要表示字符串 I'm OK ,这时,可以用" "括起来表示 ...

  3. mysql 权限管理

     参考:    http://www.cnblogs.com/Richardzhu/p/3318595.html 一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内 ...

  4. Oracle创建触发器实现主键自增

    CREATE OR REPLACE TRIGGER "trigger_empl" before insert on extjsTest1.t_empl for each row b ...

  5. hibernate简介(Session,几种状态,方法······等)

    1.Hibernate是什么?          Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数 ...

  6. sublime 配置

    Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...

  7. iOS socket编程 第三方库 AsyncSocket(GCDAsyncSocket)

    Socket描述了一个IP.端口对.它简化了程序员的操作,知道对方的IP以及PORT就可以给对方发送消息,再由服务器端来处理发送的这些消息.所以,Socket一定包含了通信的双发,即客户端(Clien ...

  8. Linux rm命令

    rm可以用来删除文件和文件夹.  rm --help Usage: rm [OPTION]... FILE... Remove (unlink) the FILE(s).   -f, --force ...

  9. Java Swing 快捷键

    Java  Swing 快捷键 给Java Swing 编程中按钮或者其他组件事件添加快捷键的方法: Component.setAccelerator(KeyStroke.getKeyStroke(‘ ...

  10. 使用Web代理实现Ajax跨域

    目前的工作项目分为前端和后台,双方事先约定接口,之后独立开发.后台每天开发完后在测试服务器上部署,前端连接测试服务器进行数据交互.前端和后台分开的好处是代码不用混在一个工程里一起build,互不干涉. ...