第4章 Selenium2-java WebDriver API (二)

4.8  定位一组元素

 定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词element后面多了一个s表示复数。定位一组元素一般用于以下场景:

·批量操作元素,例如勾选页面上所有的复选框。

·先获取一组元素,再从这组对象中过滤出需要操作的元素。例如定位出页面上所有的checkbox,然后选择其中的一个进行操作。

eg:

编写一个html页面。

<html>
 <head>
 <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
   <title>checkbox</title>
   <link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.main.css" rel="stylesheet"/>
   <script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.main.js"></script>
   <style>
      .well{
        width:200px;
        height:300px;
        margin:40 auto;
        border:1px solid red;
      }
      .control-group{
        margin-top:30px;
      }
      .controls{
       margin-left:100px;
       margin-top:-15px;
      }
   </style>
   
  </head>
 <body>
  
  <div class="well">
  <h3>checkbox</h3>
   <form class="form-horizontal">
    <div class="control-group">
     <label class="control-label" for="c1">checkbox1</label>
     <div class="controls">
      <input type="checkbox" id="c1"/>
     </div>
     </div>
     
     <div class="control-group">
     <label class="control-label" for="c2">checkbox1</label>
     <div class="controls">
      <input type="checkbox" id="c2"/>
     </div>
     </div>
     
     <div class="control-group">
     <label class="control-label" for="c3">checkbox1</label>
     <div class="controls">
      <input type="checkbox" id="c3"/>
     </div>
     </div>
   </form>
  </div>
 </body>
</html>

java代码;

package com.cy.selenium;

import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test03 {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("start");
        WebDriver driver = new FirefoxDriver();
        File file = new File("C:/Users/Administrator/Desktop/test.html");
        String filePath = file.getAbsolutePath();
        driver.get(filePath);
        // 通过css查找一组元素
        List<WebElement> inputs = driver.findElements(By.cssSelector("input[type=checkbox]"));
        for (WebElement checkbox : inputs) {
            checkbox.click();
        }
        Thread.sleep(3000);
        // 刷新
        driver.navigate().refresh();
        
        // 选择最后一个checkbox  通过xpath查找一组元素
        List<WebElement> checkboxs = driver.findElements(By.xpath(".//input[@type='checkbox']"));
        checkboxs.get(checkboxs.size()-1).click();
        Thread.sleep(2000);
        driver.close();

    }
}

效果:

4.9  多表单切换

<html>
 <head>
 </head>
 <body>
  <div class="row_fluid">
   <div class="span10 well">
    <h3>frame</h3>
    <iframe id="if" name="nf" src="http://www.baidu.com" width="1200px" height="300px"></iframe>
   </div>
  </div> 
 </body> 
</html>

java代码:

package com.cy.selenium;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Iframe {
    
    public static void main(String[] args) throws InterruptedException {
        System.out.println("start");
        WebDriver driver = new FirefoxDriver();
        File file = new File("C:/Users/Administrator/Desktop/frame.html");
        String filePath = file.getAbsolutePath();
        driver.get(filePath);
        
        // 切换到iframe (id="if")
        driver.switchTo().frame("if");
        driver.findElement(By.id("kw")).sendKeys("webDrive");
        driver.findElement(By.id("su")).click();
        Thread.sleep(4000);
        driver.close();
        //退回上一级表单
    //    driver.switchTo().defaultContent();

        
    }

}

4.10  多窗口切换

4.11  警告框处理

 在WebDriver中处理JavaScript所生成的alert、confirm以及prompt十分简单,具体做法是使用 switch_to_alert()方法定位到 alert/confirm/prompt,然后使用text/accept/dismiss/ sendKeys等方法进行操作。

·getText():返回 alert/confirm/prompt 中的文字信息。

·accept(): 接受现有警告框。
·dismiss():解散现有警告框。
·sendKeys(keysToSend):    发送文本至警告框。keysToSend:将文本发送至警告框。

eg:

package com.cy.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Alert {
  public static void main(String[] args) throws InterruptedException {
      System.out.println("start selenium");
        WebDriver driver=new FirefoxDriver();// 用WebDriver new Firefox浏览器的驱动给变量driver,相当于driver拿到了Firefox浏览器的控制权。
        driver.get("http://www.baidu.com/");
        Actions action=new Actions(driver);
        // 悬停在设置上
        action.clickAndHold(driver.findElement(By.linkText("设置"))).perform();
        // 打开搜索设置
        driver.findElement(By.className("setpref")).click();
        Thread.sleep(3000);
        //保存设置
        driver.findElement(By.className("prefpanelgo")).click();
        Thread.sleep(2000);
        // 接受弹框
        driver.switchTo().alert().accept();
        
        Thread.sleep(2000);
        
        driver.close();
        
        
}
}
原文地址:https://www.cnblogs.com/hellokitty1/p/6343941.html