selenium之二

参考网址:
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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值