Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用driver.quit关闭不同浏览器的实例_Java_Selenium_Quit - Fatal编程技术网

Java 无法使用driver.quit关闭不同浏览器的实例

Java 无法使用driver.quit关闭不同浏览器的实例,java,selenium,quit,Java,Selenium,Quit,我打开了不同的浏览器实例,最后我想关闭所有实例,但当我使用driver.close()或driver.quit()时,它只是关闭浏览器的最后一个实例。请帮忙 import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.Inter

我打开了不同的浏览器实例,最后我想关闭所有实例,但当我使用driver.close()或driver.quit()时,它只是关闭浏览器的最后一个实例。请帮忙

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;


public class showClose {

static WebDriver driver;

public showClose(WebDriver driver){
    this.driver=driver;
}

public static void main(String[] args) {

    showClose sc = new showClose(driver);
    sc.IE("http://www.msn.com");
    sc.Firefox("http://seleniumhq.org");
    sc.Chrome("http://google.com");

    driver.quit();

}

//Internet Explorer driver
public void IE(String URL){
    //Set the driver property for IE
    System.setProperty("webdriver.ie.driver",                 System.getProperty("user.dir")+"\\IEDriverServer.exe");

    DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();  
    ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

    //Create object of Internet explorer driver
    driver = new InternetExplorerDriver(ieCapabilities);
    driver.get(URL);
}

//Firefox driver
public void Firefox(String URL){
    driver = new FirefoxDriver();
    driver.get(URL);
}

//Chrome driver
public void Chrome(String URL){
    System.setProperty("webdriver.chrome.driver",      System.getProperty("user.dir")+"\\chromedriver.exe");

    driver = new ChromeDriver();
    driver.get(URL);

}
}

首先,如果要关闭所有窗口,应该使用
driver.quit()
driver.close()
用于单个窗口。 也许这与浏览器窗口试图关闭时发出警报有关


在每次调用“sc.IE”、“sc.Firefox”或“sc.Chrome”时,您都会覆盖实例变量“driver”。 因此,唯一一个被调用“driver.quit”关闭的驱动程序是最后一个。 如果您想在访问URL后关闭浏览器,则必须在每次调用“sc.IE”、“sc.Firefox”或“sc.Chrome”之前在中执行“driver.quit”操作,或者管理Web驱动程序列表并关闭所有驱动程序。 例如,您可以这样做:

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class ShowClose {

private List<WebDriver> drivers;

public ShowClose(){
    this.drivers = new ArrayList<WebDriver>();
}

public static void main(String[] args) {

    ShowClose sc = new ShowClose();
    sc.IE("http://www.msn.com");
    sc.Firefox("http://seleniumhq.org");
    sc.Chrome("http://google.com");

    sc.CloseAll();
}

public void CloseAll() {
    for(WebDriver d : drivers) {
        d.quit();
    }
}

//Internet Explorer driver
public void IE(String URL){
    //Set the driver property for IE
    System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");

    DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();  
    ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

    //Create object of Internet explorer driver
    WebDriver driver = new InternetExplorerDriver(ieCapabilities);
    driver.get(URL);
    this.drivers.add(driver);
}

//Firefox driver
public void Firefox(String URL){
    WebDriver driver = new FirefoxDriver();
    driver.get(URL);
    this.drivers.add(driver);
}

//Chrome driver
public void Chrome(String URL){
    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.get(URL);
    this.drivers.add(driver);
}
import java.util.ArrayList;
导入java.util.List;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.ie.InternetExplorerDriver;
导入org.openqa.selenium.remote.DesiredCapabilities;
公开课收场{
私人名单司机;
公开展示(){
this.drivers=newarraylist();
}
公共静态void main(字符串[]args){
ShowClose sc=新的ShowClose();
理学士(”http://www.msn.com");
sc.Firefox(“http://seleniumhq.org");
sc.Chrome(“http://google.com");
sc.CloseAll();
}
公共空间关闭所有(){
for(WebDriver d:驱动程序){
d、 退出();
}
}
//Internet Explorer驱动程序
公共无效IE(字符串URL){
//设置IE的驱动程序属性
System.setProperty(“webdriver.ie.driver”,System.getProperty(“user.dir”)+“\\IEDriverServer.exe”);
DesiredCapabilities ieCapabilities=DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.include\u flakines\u通过忽略安全域,true);
//创建Internet explorer驱动程序的对象
WebDriver驱动程序=新的InternetExplorerDriver(IECCapabilities);
获取(URL);
this.drivers.add(驱动程序);
}
//Firefox驱动程序
公共无效Firefox(字符串URL){
WebDriver=newfirefoxdriver();
获取(URL);
this.drivers.add(驱动程序);
}
//镀铬驱动器
公共无效Chrome(字符串URL){
System.setProperty(“webdriver.chrome.driver”,System.getProperty(“user.dir”)+“\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
获取(URL);
this.drivers.add(驱动程序);
}

}步骤1:

在主类中声明'List'接口并将其声明为'Static'

public static  List<WebDriver> drivers;
步骤4:在main类中,将存储的驱动程序的所有实例复制到一个对象,并使用该对象关闭所有打开的实例

for(WebDriver d : drivers)
{
d.quit();
}
this.drivers.add(driver);
for(WebDriver d : drivers)
{
d.quit();
}