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. 数论 UVALive 2911

    这道题是一道数论题. 题目的意思是告诉m.p.a.b,并且告诉你xi满足的两个条件.让你求出 xp1 + xp2 +...+ xpm 的最大值(其中p<=12,切p是偶数). 这里需要对于xi所 ...

  2. sublime 中文乱码

      一.安装Package Control 使用Sublime Text2首先就要安装Package Control,这样就能使用丰富的插件包了 (1)访问Package Control站点按照提示复 ...

  3. USB

    [一].USB(Universal Serial Bus):是通用串行总线的缩写,具有方便易用,动态分配带宽,容错性优越和高性价比等特点. USB接口设备结构分:USB Host(主机)和USB De ...

  4. 利用html5的localStorage做一个备忘录

    实现一个便签功能,可以记录内容和写的时间,当网页从新载入,可以读取以前的记录. html文档 <!DOCTYPE html> <html> <head lang=&quo ...

  5. 如何导出和导入mysql数据(数据迁移)

    . 导出数据库数据 mysqldump -uroot -p dp_db > dumpout.sql 其中 root是账户名 dp_db是需要导出的数据库名称 dumpout.sql存储导出的数据 ...

  6. 【转】GPU 与CPU的作用协调,工作流程、GPU整合到CPU得好处

    在不少人的心目中,显卡最大的用途可能就只有两点--玩游戏.看电影,除此之外,GPU并没有其他的作用了.但是随着微软IE9的正式发布,不少人突然发现,微软一直提到一个名词:GPU硬件加速,从而也让不少人 ...

  7. 解决ideviceinstaller未安装的问题

    在Mac上,使用Appium时提示: Could not initialize ideviceinstaller; make sure it is installed and works on you ...

  8. JQuery实现列表中复选框全选反选功能封装

    我们在做列表的时候经常会遇到全选,反选进行批量处理问题,例如: 我当时就是简单的实现了,然后想封装到公共的js中,封装的太烂,不好意思贴出来了(就是把实现代码之间放到公共js中,然后每个页面都用固定的 ...

  9. 【linux】 解决linux下vsftp 500 OOPS: cannot change directory:/home/ftp/ 办法

    用FileZilla连接ftp出现错误,500 OOPS: cannot change directory:/home/ftp 原因是CentOS系统安装了SELinux,因为默认下是没有开启FTP的 ...

  10. [综] Latent Dirichlet Allocation(LDA)主题模型算法

    多项分布 http://szjc.math168.com/book/ebookdetail.aspx?cateid=1&&sectionid=983 二项分布和多项分布 http:// ...