Selenium(五)——webdriver 之操作頁面元素

轉自http://blog.sina.com.cn/s/blog_6966650401012a7q.html

一、輸入框(text field or textarea)
//找到輸入框元素:
WebElement element = driver.findElement(By.id("passwd-id"));
//將輸入框清空:
element.clear();
//在輸入框中輸入內容:
element.sendKeys(“test”);
//獲取輸入框的文本內容:
element.getText();

二、下拉選擇框(Select)
//找到下拉選擇框的元素:
Select select = new Select(driver.findElement(By.id("select")));

//選擇對應的選擇項:
select.selectByVisibleText(“mediaAgencyA”);

select.selectByValue(“MA_ID_001”);

//不選擇對應的選擇項:
select.deselectAll();
select.deselectByValue(“MA_ID_001”);
select.deselectByVisibleText(“mediaAgencyA”);
或者獲取選擇項的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();

對下拉框進行操作時首先要定位到這個下拉框,new 一個Selcet對象,然後對它進行操作

例如:
以http://passport.51.com/reg2.5p這個頁面爲例。這個頁面中有4個下拉框,下面演示4種選中下拉框選項的方法。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SelectsStudy {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();
dr.get("http://passport.51.com/reg2.5p");

//通過下拉列表中選項的索引選中第二項,即2011年
Select selectAge = new Select(dr.findElement(By.id("User_Age")));
selectAge.selectByIndex(2);//Select.selectByIndex

//通過下拉列表中的選項的value屬性選中"上海"這一項
Select selectShen = new Select(dr.findElement(By.id("User_Shen")));
selectShen.selectByValue("上海");//Select.selectByValue

//通過下拉列表中選項的可見文本選 中"浦東"這一項
Select selectTown = new Select(dr.findElement(By.id("User_Town")));
selectTown.selectByVisibleText("浦東");//Select.selectByVisibleText

//這裏只是想遍歷一下下拉列表所有選項,用click進行選中選項
Select selectCity = new Select(dr.findElement(By.id("User_City")));
for(WebElement e : selectCity.getOptions())//Select.getOptions()
e.click();
}
}

三、單選項(Radio Button)
//找到單選框元素:
WebElement bookMode =driver.findElement(By.id("BookMode"));
//選擇某個單選項:
bookMode.click();
//清空某個單選項:
bookMode.clear();
//判斷某個單選項是否已經被選擇:
bookMode.isSelected();

四、多選項(checkbox)
//多選項的操作和單選的差不多:
WebElement checkbox =driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();

五、按鈕(button)
//找到按鈕元素:
WebElement saveButton = driver.findElement(By.id("save"));
//點擊按鈕:
saveButton.click();
//判斷按鈕是否enable:
saveButton.isEnabled ();

六、左右選擇框
也就是左邊是可供選擇項,選擇後移動到右邊的框中,反之亦然。
例如:
Select lang = new Select(driver.findElement(By.id("languages")));
lang.selectByVisibleText(“English”);
WebElement addLanguage =driver.findElement(By.id("addButton"));
addLanguage.click();

七、彈出對話框(Popup dialogs)
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();

八、表單(Form)
Form中的元素的操作和其它的元素操作一樣,對元素操作完成後對錶單的提交可以:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();

approve.submit();//只適合於表單的提交

九、上傳文件 (Upload File)
上傳文件的元素操作:
WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);


十、拖拉(Drag andDrop)
WebElement element =driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();

例如:下面這個頁面是一個演示拖放元素的頁面,你可以把左右頁面中的條目拖放到右邊的div框中。
http://koyoz.com/demo/html/drag-drop/drag-drop.html

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

public class DragAndDrop {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();
dr.get("http://koyoz.com/demo/html/drag-drop/drag-drop.html");

//首先new出要拖入的頁面元素對象和目標對象,然後進行拖入。
WebElement element = dr.findElement(By.id("item1"));
WebElement target = dr.findElement(By.id("drop"));
(new Actions(dr)).dragAndDrop(element, target).perform();

//利用循環把其它item也拖入
String id="item" ;
for(int i=2;i<=6;i++){
String item = id+i;
(new Actions(dr)).dragAndDrop(dr.findElement(By.id(item)), target).perform();
}
}
}
代碼很簡單,需要注意的是(new Actions(dr)).dragAndDrop(element, target).perform();這句話中,dragAndDrop(element, target)這個方法是定義了“點擊element元素對象,然後保持住,直到拖到目標元素對象裏面才鬆開”這一系列動作的Actions,如果你不調用perform()方法,這個Actions是不會執行的。


十一、導航 (Navigationand History)
//打開一個新的頁面:
driver.navigate().to("http://www.example.com");
//通過歷史導航返回原頁面:
driver.navigate().forward();
driver.navigate().back();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章