最大化、前进、后退、刷新
public static void main(String[] args) {
System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)\\Google\\Chrome\\Application");
WebDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com/");
WebElement search_text =driver.findElement(By.id("kw"));
search_text.sendKeys("博客园");
search_text.submit();
driver.manage().window().maximize();
//浏览器窗口最大化
driver.navigate().back();
//浏览器前进
driver.navigate().forward();
//浏览器后退
driver.navigate().refresh();
//浏览器刷新
}
获取窗口句柄、切换窗口
public static void main(String[] args) {
System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)\\Google\\Chrome\\Application");
WebDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com/");
WebElement i = driver.findElement(By.xpath("//*[@id=\"lg\"]/map/area"));
i.click();
String window1 = driver.getWindowHandle();
System.out.println(window1);
//获取浏览器当前窗口句柄
Set<String> windows = driver.getWindowHandles();
//获取浏览器所有窗口,用set集合接收
String window2 = null;
Iterator<String> set = windows.iterator();
while (set.hasNext()) {
String ss = set.next();
if (!ss.equals(window1)) {
window2 = ss;
}
}
//使用迭代器遍历set集合
System.out.println(window1);
//打印窗口1的句柄
System.out.println(window2);
//打印窗口2的句柄
driver.switchTo().window(window2);
//切换到窗口2
driver.switchTo().window(window1);
//切换到窗口1
}
关闭当前窗口、关闭浏览器
public static void main(String[] args) {
System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)\\Google\\Chrome\\Application");
WebDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com/");
WebElement i = driver.findElement(By.xpath("//*[@id=\"lg\"]/map/area"));
i.click();
driver.close();
//关闭当前窗口
driver.quit();
//关闭浏览器
}