找到輸入框元素:web
WebElement element = driver.findElement(By.id("passwd-id"));cookie
在輸入框中輸入內容:app
element.sendKeys(「test」);dom
將輸入框清空:ide
element.clear();url
獲取輸入框的文本內容:spa
element.getText();orm
找到下拉選擇框的元素:ip
Select select = new Select(driver.findElement(By.id("select")));ci
選擇對應的選擇項:
select.selectByVisibleText(「mediaAgencyA」);
或
select.selectByValue(「MA_ID_001」);
不選擇對應的選擇項:
select.deselectAll();
select.deselectByValue(「MA_ID_001」);
select.deselectByVisibleText(「mediaAgencyA」);
或者獲取選擇項的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();
找到單選框元素:
WebElement bookMode =driver.findElement(By.id("BookMode"));
選擇某個單選項:
bookMode.click();
清空某個單選項:
bookMode.clear();
判斷某個單選項是否已經被選擇:
bookMode.isSelected();
多選項的操做和單選的差很少:
WebElement checkbox =driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();
找到按鈕元素:
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();
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();
Form中的元素的操做和其它的元素操做同樣,對元素操做完成後對錶單的提交能夠:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
或
approve.submit();//只適合於表單的提交
上傳文件的元素操做:
WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
通常來講,登陸後建議是先:
driver.switchTo().defaultContent();
切換到某個frame:
driver.switchTo().frame("leftFrame");
從一個frame切換到另外一個frame:
driver.switchTo().frame("mainFrame");
切換到某個window:
driver.switchTo().window("windowName");
WebElement element =driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
打開一個新的頁面:
driver.navigate().to("http://www.example.com");
經過歷史導航返回原頁面:
driver.navigate().forward();
driver.navigate().back();
User Agent的設置是平時使用得比較多的操做:
FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override","some UA string");
WebDriver driver = new FirefoxDriver(profile);
咱們常常要對的值進行讀取和設置。
增長cookie:
// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);
獲取cookie的值:
// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
System.out.println(String.format("%s -> %s",loadedCookie.getName(), loadedCookie.getValue()));
}
根據某個cookie的name獲取cookie的值:
driver.manage().getCookieNamed("mmsid");
刪除cookie:
// You can delete cookies in 3 ways
// By name
driver.manage().deleteCookieNamed("CookieName");
// By Cookie
driver.manage().deleteCookie(loadedCookie);
// Or all of them
driver.manage().deleteAllCookies();
Web driver對Java Script的調用是經過JavascriptExecutor來實現的,例如:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("(function(){inventoryGridMgr.setTableFieldValue('"+ inventoryId + "','" + fieldName + "','"
+ value + "');})()");
若是用webdriver截圖是:
driver = webdriver.Firefox()
driver.save_screenshot("C:\error.jpg");
由於Load頁面須要一段時間,若是頁面還沒加載完就查找元素,必然是查找不到的。最好的方式,就是設置一個默認等待時間,在查找頁面元素的時候若是找不到就等待一段時間再找,直到超時。
Webdriver提供兩種方法,一種是顯性等待,另外一種是隱性等待。
顯性等待:
WebDriver driver =new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
returnd.findElement(By.id("myDynamicElement"));
}});
隱性等待:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement =driver.findElement(By.id("myDynamicElement"));