java selenium (十) 操作浏览器

时间:2023-03-09 09:35:56
java selenium (十) 操作浏览器

本篇文章介绍selenium 操作浏览器

阅读目录

浏览器最大化 前进,后退, 刷新

    public static void testBrowser(WebDriver driver) throws Exception
{
driver.get("http://www.cnblogs.com/tankxiao");
Thread.sleep(5000);
// 浏览器最大化
driver.manage().window().maximize(); driver.navigate().to("http://www.baidu.com");
// 刷新浏览器
driver.navigate().refresh();
// 浏览器后退
driver.navigate().back();
// 浏览器前进
driver.navigate().forward();
// 浏览器退出
driver.quit();
}

截图操作

    public static void testScreenShot(WebDriver driver) throws Exception
{
driver.get("http://www.baidu.com");
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("c:\\1.png"));
}

模拟鼠标操作

    public static void rightClickMouse(WebDriver driver)
{
driver.get("http://www.baidu.com");
Actions action = new Actions(driver); action.contextClick(driver.findElement(By.id("kw"))).perform();
}

杀掉Windows浏览器进程

    public static void killProcess()
{
// kill firefox
WindowsUtils.tryToKillByName("firefox.exe");
// kill IE
WindowsUtils.tryToKillByName("iexplore.exe");
// kill chrome
WindowsUtils.tryToKillByName("chrome.exe");
}

操作cookie