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

selenium2.0(WebDriver) API的更多相关文章

  1. Selenium2.0 Webdriver 随笔

    Webdriver can't action the element when the element is out of view 1. Scroll to the element use Java ...

  2. Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考

    Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考 //System.setProperty("webdriver.firefox.bin" ...

  3. 在selenium2.0中使用selenium1.0的API

    Selenium2.0中使用WeDriver API对页面进行操作,它最大的优点是不需要安装一个selenium server就可以运行,但是对页面进行操作不如selenium1.0的Selenium ...

  4. selenium2(WebDriver) API

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

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

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

  6. Python版:Selenium2.0之WebDriver学习总结_实例1

    Python版:Selenium2.0之WebDriver学习总结_实例1  快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 实属转载:本人看的原文地址 :http:/ ...

  7. Webdriver API (二)

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

  8. Webdriver API (一)

    (转载) 1.1  下载selenium2.0的包 官方download包地址:http://code.google.com/p/selenium/downloads/list 官方User Guid ...

  9. selenium2.0处理case实例(二)

    本文通过具体代码处理过程, 来展示selenium中一些比较不常用的类的用法 1.javascriptExcutor,通过将driver强转成JavascriptExecutor类型, 调用execu ...

随机推荐

  1. frame与frame之间怎么用jquery传值

    frame与frame之间如何用jquery传值 使用jquery操作iframe 1. 内容里有两个ifame <iframe id="leftiframe"...< ...

  2. JSPatch心得

    转载请注明来源 1 CGSize,CGPoint,CGRect的使用,直接调用x,y,width,height四个属性,而且不用加().例子defineClass('PZPhotoView', { z ...

  3. SVN更新报错

    将服务器SVN文件更新到本地是出现下图错误 报错中已经提示可以通过clean up来清理,若直接执行release lock,则不会解决问题. 原因:本地的项目中存在过期的工作副本 解决办法:选择该文 ...

  4. appium +python 一个简单的例子

    appium 安装和python 安装好后. 1.      启动android模拟器--Genymotion-点击Start 2.      启动appium 3.     运行代码. # -*- ...

  5. iOS测试常见崩溃

    什么是崩溃日志,从哪里能得它? iOS设备上的应用闪退时,操作系统会生成一个崩溃报告,也叫崩溃日志,保存在设备上.崩溃日志上有很多有用的信息,包括应用是什么情况下闪退的.通常,上面有每个正在执行线程的 ...

  6. create dll project based on the existing project

    Today, I have to create a dll project(called my.sln), the dllmain.cpp/.h/ is already in another proj ...

  7. 【转载】ANSYS TRANSIENT ANSLYSIS [2]

    原文地址:http://sps.utm.my/wp-content/uploads/2014/12/ANSYS-day2-Transient-analysis.pdf

  8. Eclipse动态web工程(Dynamic Web Project)添加jar文件的正确方法

    Eclipse中,创建了动态web工程之后,如果需要添加新的jar文件,有两种方法.第一种是配置工程的“build path”,第二种则是将jar文件放在工程目录下的“/WebContent/WEB- ...

  9. 2016_09_21 Russia is seriously running out of cash_CNN

    After almost two years in recession,the country's rainy day fund has shrunk to just $32.2 billlion t ...

  10. java关键字:synchronized

    JAVA 如何共享资源 关于synchronized函数: java具有内置机制,可防止某种资源(此处指的是对象的内存内容)冲突.由于你通常会将某class的数据元素声明为private,并且只经由其 ...