Selenium对浏览的关闭操作,在driver中有两种方法:一个是quit,另一个叫close。先来看看看两者的声明描述,请看下面声明文档。
/**
* Close the current window, quitting the browser if it's the last window currently open.
*/
void close();
/**
* Quits this driver, closing every associated window.
*/
void quit();
从上面的声明文档中,可以知道:close方法是关闭当前窗口。(当前窗口的意思就是表示driver现在正在操作的窗口)如果当前窗口只有一个tab,那么这个close方法就相当于关闭了浏览器。quit方法就直接关闭是直接退出并关闭浏览器所有打开的tab窗口。所以,close方法一般关闭一个tab,quit方法才是我们认为的完全关闭浏览器方法。
下面实例表示的就是浏览器只有一个tab的时候,close方法就直接关闭浏览器了。
package com.keydom;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selnium01 {
public static void main(String[] args) throws InterruptedException {
//设置chromedriver驱动位置,因为chromedriver没有在eclipse目录下
System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
Thread.sleep(2000);
driver.close();
}
}
下面实例中浏览器有两个tab,此时close方法就只能关闭driver正在操作的窗口而不能关闭浏览器了。
package com.keydom;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class selnium01 {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
//设置chromedriver驱动位置,因为chromedriver没有在eclipse目录下
System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
Thread.sleep(2000);
driver.findElement(By.id("kw")).sendKeys("中国");
Thread.sleep(2000);
driver.findElement(By.id("su")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a")); //定位目标控件
String url = element.getAttribute("href"); //获取目标控件的url
((JavascriptExecutor)driver).executeScript("window.open('"+url+"')"); //打开目标链接
Thread.sleep(1000);
changeWindow();
Thread.sleep(1000);
driver.close();
}
/**
* 切换窗口
*/
public static void changeWindow() {
String currWindow = driver.getWindowHandle(); //获取当前窗口句柄
Set<String> allWindows = driver.getWindowHandles(); //获取当前浏览器的所有窗口句柄
Iterator<String> it = allWindows.iterator();
while(it.hasNext()) {
driver.switchTo().window(it.next());
}
}
}
下面实例中浏览器有两个tab,用quit方法就是关闭是直接退出并关闭浏览器所有打开的两个tab窗口。package com.keydom;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class selnium01 {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
//设置chromedriver驱动位置,因为chromedriver没有在eclipse目录下
System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
Thread.sleep(2000);
driver.findElement(By.id("kw")).sendKeys("中国");
Thread.sleep(2000);
driver.findElement(By.id("su")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a")); //定位目标控件
String url = element.getAttribute("href"); //获取目标控件的url
((JavascriptExecutor)driver).executeScript("window.open('"+url+"')"); //打开目标链接
Thread.sleep(1000);
changeWindow();
Thread.sleep(1000);
driver.quit();
}
/**
* 切换窗口
*/
public static void changeWindow() {
String currWindow = driver.getWindowHandle(); //获取当前窗口句柄
Set<String> allWindows = driver.getWindowHandles(); //获取当前浏览器的所有窗口句柄
Iterator<String> it = allWindows.iterator();
while(it.hasNext()) {
driver.switchTo().window(it.next());
}
}
}