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());