seleniumWebDriver的API操作杀进程、截屏、执行js脚本_10_4
1.杀掉windows的浏览器进程
public void f() throwsInterruptedException {
Thread.sleep(3000);
//杀掉windows进程中的Firefox浏览器进程,关闭所有Firefox浏览器
WindowsUtils.tryToKillByName("firefox.exe");
//杀掉windows进程中的Firefox浏览器进程,关闭所有iexplore浏览器
WindowsUtils.tryToKillByName("iexplore.exe");
//杀掉windows进程中的Firefox浏览器进程,关闭所有chrome浏览器
WindowsUtils.tryToKillByName("chrome.exe");
}
2.将当前浏览器的窗口截屏
//访问sogou首页
driver.get("http://www.sogou.com");
//调用getScreenshotAs方法把当前浏览器打开页面进行截图,保存到一个File对象中
FilescrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try{
//把File对象转换成一个保存在F:\SeleniumWebDriver\workspace\WebDriverAPI0302\images目录名为test.png的图片文件
FileUtils.copyFile(scrFile,newFile("F:\\SeleniumWebDriver\\workspace\\WebDriverAPI0302\\images\\test.png"));
}catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
}
3.执行JavaScript脚本
driver.get("http://www.sogou.com");
//声明一个JavaScript执行器对象
JavascriptExecutor js=(JavascriptExecutor)driver;
//调用执行器对象的executeScript方法来执行JvaScript脚本return document。title
String title=(String)js.executeScript("returndocument.title");
System.out.println(title.toString());