参考网址:
http://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example
1.Moving Between Windows and Frames
driver.switchTo().window("window 名称");
driver.switchTo().frame("frameName");
但是如何知道一个窗口的名称呢?
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
可以在各个打开的窗口间切换
for(String handle: driver.getWindowHandles())
{
driver.switchTo().window(handle);
}
2.Popup Dialogs
当触发某个动作,弹出一个弹窗的时候, 下面的代码可以进入到那个弹窗。
Alert alert = driver.swithTo().alert();
关于弹窗,还有accept(), dismiss(), getText(), sendKeys(), 等等方法。
3.Navigation: History and Location
driver.navigate().to("http://www.example.com");
driver.navigate().forward();
driver.navigate().back();
4.Cookies
// Go to the correct domain
driver.get("http://www.example.com");
// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(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()));
}
// 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();
5.changing the user agent
更具体可参考:
http://www.seleniumhq.org/docs/03_webdriver.jsp#firefox-driver
FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "some UA string");
WebDriver driver = new FirefoxDriver(profile);
6.Drag And Drop
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();