轉自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 {
}
三、單選項(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 {
}
代碼很簡單,需要注意的是(new Actions(dr)).dragAndDrop(element, target).perform();這句話中,dragAndDrop(element, target)這個方法是定義了“點擊element元素對象,然後保持住,直到拖到目標元素對象裏面才鬆開”這一系列動作的Actions,如果你不調用perform()方法,這個Actions是不會執行的。
十一、導航 (Navigationand History)
//打開一個新的頁面:
//通過歷史導航返回原頁面:
driver.navigate().forward();
driver.navigate().back();