Selenium2.0功能測(cè)試之Web元素的定位
頁(yè)面元素的定位可以說(shuō)是WebDriver中最核心的內(nèi)容了,我們定位元素的目的主要有:操作元素,獲取該元素的屬性,獲取元素的text以及獲取元素的數(shù)量,WebDriver 為我們提供了以下幾種方法來(lái)幫我們定位web元素:
通過(guò)元素的id獲取
通過(guò)元素的name獲取
通過(guò)元素的tag name 獲取
通過(guò)css xpath 獲取
通過(guò)xpath 獲取
通過(guò)class name 獲取
通過(guò)一部分的link text 獲取元素
通過(guò)全部的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"; // 需要更改這個(gè)URL到你自己的login.html 的文件路徑 public static void main(String[] args) throws InterruptedException { WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); //最大化瀏覽器界面 driver.get(URL); //訪(fǎng)問(wèn)谷哥的首頁(yè) ,此處放棄度娘。 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 } } |
字體: 小 中 大 | 上一篇 下一篇 | 打印 | 我要投稿
這里是要測(cè)試的頁(yè)面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 } } |
測(cè)試頁(yè)面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) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): selenium and watir webdrivers 自動(dòng)化測(cè)試學(xué)習(xí)