(转载)

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. AOP和IOC个人理解

    14:18 2014/5/5 IOC inversion of control 控制反转  将new对象的权力由调用者转移到spring容器(即xml文件),Struts2与Spring整合(scop ...

  2. python学习笔记27(python中sys模块的使用)

    sys.argv           命令行参数List,第一个元素是程序本身路径 sys.modules.keys() 返回所有已经导入的模块列表 sys.exc_info()     获取当前正在 ...

  3. ios App 打包

    ios 版本的 App 打包两种方式: 1. 命令行 xcodebuild exportArchive -exportFormat ipa 2. 通过 xcode Product -> Arch ...

  4. iOS 添加阴影后 屏幕卡顿 抖动

    - (void)awakeFromNib { // Initialization code _btnViews.layer.shadowPath =[UIBezierPath bezierPathWi ...

  5. NOI2015考试小结

    这次NOI2015有幸获得金牌考进了国家集训队,意味着我的OI退役时间既省选之后有延迟了好几个月,又有了新的目标吧. 先说一下考试之外的感受吧,学军宿舍很牛X,接待NOIers而不提供插座,唯一可以用 ...

  6. EasyUI datagrid 分页Json字符串格式

    //EasyUI datagrid 分页Json字符串格式 //{"total":xx,"rows":[{...},{...}]} total:总数 rows: ...

  7. ECNU-2574 Principles of Compiler

    题意: 给出编译规则,求是否满足条件 A:= '(' B')'|'x'.    B:=AC.    C:={'+'A}. 其中{}表示里面的内容可以出现0次或者多次 注意点见代码注释 #include ...

  8. Android ListView相关 fastScrollEnabled

    1.android:fastScrollEnabled="true" 当数据量比较多的时候,右侧会出现一个可以拉动的滚动条,这样可以很快的拉动,如图:

  9. java中 正则表达式的使用

    推荐使用第一种 第一种: //对接收的文件名的合法性进行验证 String fileName="127.0.0.1_01_20140428165022174.jpg"; Strin ...

  10. SQL盲注修订建议

    一般有多种减轻威胁的技巧: [1] 策略:库或框架 使用不允许此弱点出现的经过审核的库或框架,或提供更容易避免此弱点的构造. [2] 策略:参数化 如果可用,使用自动实施数据和代码之间的分离的结构化机 ...