Selenium中quit和close的区别

本文探讨了Selenium中driver.quit()和driver.close()的区别。quit()方法将关闭所有窗口并终止浏览器进程,而close()仅关闭当前窗口,若为唯一窗口则浏览器也会退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值