您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源功能測(cè)試工具 > Selenium
Selenium2.0功能測(cè)試之Web元素的定位
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/10/22 9:45:16 ] 推薦標(biāo)簽:

  這里是要測(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
}
}

上一頁(yè)123下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd