webdriver常用技巧(java)

场景一:鼠标滑动
element=driver.findElement(By. cssSelector(".user-info.right>div>p>a"));
Actions builder = newActions(driver);
builder.moveToElement(element).build().perform();
//Thread.sleep(100); no need
element=driver.findElement(By. cssSelector(".frd-del"));
element.click();

场景二:加载ua
privatestaticWebDriver driver;
FirefoxProfile profile = newFirefoxProfile();
profile.setPreference("general.useragent.override",
"Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
driver= newFirefoxDriver(profile);

场景三:页面切换
String pWindUrl = driver.getCurrentUrl();
//跳至新打开的窗口
if( driver.getWindowHandles().size() != 2) { fail(); }
for(String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
if( driver.getCurrentUrl().equals(pWindUrl)){
driver.close();//关闭父窗口
} else{
//6张图片
if( driver.findElements(By. cssSelector(".img")).size() != 6) { fail(); }
}
}

场景四:alert提示
场景五:截屏
privateAlertalert;
alert=driver.switchTo().alert();
alert.accept();
//alert.dismiss();

场景五:截屏
try{
File scrFile = ((TakesScreenshot)driver)
.getScreenshotAs(OutputType. FILE);
FileUtils. copyFile(scrFile, newFile("img\\Screenshot.png"));
} catch(Exception e) {
e.printStackTrace();
} finally{
// driver.close();
}

你可能感兴趣的:(webdriver)