qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Selenium2.0功能測試之Web元素的定位

           頁面元素的定位可以說是WebDriver中最核心的內容了,我們定位元素的目的主要有:操作元素,獲取該元素的屬性,獲取元素的text以及獲取元素的數量,WebDriver 為我們提供了以下幾種方法來幫我們定位web元素:
            通過元素的id獲取
            通過元素的name獲取
            通過元素的tag name 獲取
            通過css xpath 獲取
            通過xpath 獲取
            通過class name  獲取
            通過一部分的link text 獲取元素
            通過全部的link text 獲取元素
            唯一元素的定位:
          package org.coderinfo.demo;
          import org.openqa.selenium.By;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.chrome.ChromeDriver;
          public class FindSingleElements {
          private static final String URL = "file:///C:/Desktop/Selenium/login.html"; // 需要更改這個URL到你自己的login.html 的文件路徑
          public static void main(String[] args) throws InterruptedException {
          WebDriver driver = new ChromeDriver();
          driver.manage().window().maximize(); //最大化瀏覽器界面
          driver.get(URL); //訪問谷哥的首頁 ,此處放棄度娘。
          Thread.sleep(2000); //Wait for page load
          driver.findElement(By.id("inputEmail")).sendKeys("coderinfo@163.com"); // use id to find a web element
          Thread.sleep(2000);
          driver.findElement(By.name("password")).sendKeys("#####");    // use name to find a web element
          Thread.sleep(2000);
          driver.findElement(By.cssSelector("#inputEmail")).clear();   // use css selector to find a web element
          Thread.sleep(2000);
          driver.findElement(By.linkText("UseLink")).click();  // use link text to find a web element
          Thread.sleep(2000);
          driver.findElement(By.partialLinkText("Use")).click(); // use partial link text to find a web element
          Thread.sleep(2000);
          String formClassName = driver.findElement(By.tagName("form")).getAttribute("class");  //use tag name to find a web element
          System.out.println(formClassName);
          Thread.sleep(2000);
          String text = driver.findElement(By.xpath("/html/body/form/div[1]/div")).getText();  // use xpath to find a web element
          System.out.println(text);
          String inputText = driver.findElement(By.className("inputClass")).getAttribute("placeholder");  // use class name to find a web element
          System.out.println(inputText);
          Thread.sleep(5000);
          driver.quit();  //徹底退出WebDriver
          }
          }

          字體:        | 上一篇 下一篇 | 打印  | 我要投稿 

            這里是要測試的頁面login.html的源碼:
          <!DOCTYPE html>
          <html>
          <head>
          <title>For Selenium Test</title>
          <style type="text/css">
          div {
          margin-top:10px
          }
          #inputEmail {
          color:red
          }
          </style>
          </head>
          <body>
          <center>
          <h3>Find Single Element</h3>
          </center>
          <form class="form-h">
          <div class="items">
          <div class="item">
          Use ID:<input type="text" id="inputEmail" name="email" placeholder="Email"/>
          </div>
          </div>
          <div class="items">
          <div class="item">
          Use Name:<input type="password" id="inputPassword" name="password" placeholder="Password" class="inputClass"/>
          </div>
          </div>
          <div class="items">
          <div class="item">
          Use Link:<a href="#">UseLink</a>
          </div>
          </div>
          </form>
          </body>
          </html>
            一組元素的定位 :
          package org.coderinfo.demo;
          import java.util.List;
          import org.openqa.selenium.By;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.WebElement;
          import org.openqa.selenium.chrome.ChromeDriver;
          public class FindElements {
          private static final String URL = "file:///C:/user/Desktop/Selenium/checkbox.html";  //改為你自己的url
          public static void main(String[] args) {
          WebDriver driver = new ChromeDriver();  //create a chrome driver
          driver.manage().window().maximize();  // max size the chrome window
          driver.get(URL);   //open URL with the chrome browser
          try {
          Thread.sleep(2000);                  // wait for web loading
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          List<WebElement> webElements = driver.findElements(By.cssSelector("input[type='checkbox']"));  // Use css selector to get all the checkbox
          for (WebElement webElement : webElements) {   // loop through all elements
          webElement.click();    // click current element == select the current checkbox
          }
          System.out.println("Count: " + webElements.size());  //print the count of all the elements
          try {
          Thread.sleep(3000);  // wait 3s
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          webElements = driver.findElements(By.tagName("input"));  // use tag name to get all the checkbox
          webElements.get(webElements.size()-1).click();  // Cancel the last selected checkbox
          try {
          Thread.sleep(5000);  // wait 5s
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          driver.quit();  // close webdriver
          }
          }

            測試頁面checkbox.html的代碼:
          <!DOCTYPE html>
          <html>
          <head>
          <title>Get ALl CheckBox</title>
          <style type="text/css">
          h2 {
          text-align:center
          }
          </style>
          </head>
          <body>
          <h2>CheckBox<h2/>
          <form class="form-h">
          <div class="input-c">
          <input type="checkbox" class="in" id="in1"/>
          <div>
          <div class="input-c">
          <input type="checkbox" class="in" id="in2"/>
          <div>
          <div class="input-c">
          <input type="checkbox" class="in" id="in3"/>
          <div>
          <div class="input-c">
          <input type="checkbox" class="in" id="in4"/>
          <div>
          <div class="input-c">
          <input type="checkbox" class="in" id="in5"/>
          <div>
          </form>
          </body>
          </html>
          相關文章:

          posted on 2013-10-22 10:50 順其自然EVO 閱讀(334) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

          <2013年10月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 晋江市| 景德镇市| 上高县| 汪清县| 新源县| 贵港市| 绥中县| 渝北区| 库车县| 左权县| 贵溪市| 建德市| 晴隆县| 凤庆县| 拉孜县| 永善县| 枝江市| 当涂县| 鄂托克前旗| 高陵县| 海丰县| 新乡市| 恭城| 万宁市| 潜江市| 吴桥县| 易门县| 宁国市| 夏河县| 通化市| 理塘县| 保亭| 徐汇区| 随州市| 霍城县| 东丽区| 吉林市| 白玉县| 马山县| 崇州市| 华容县|