C# 关闭Windows窗体上的Chrome Web驱动程序关闭

C# 关闭Windows窗体上的Chrome Web驱动程序关闭,c#,google-chrome,selenium,selenium-webdriver,C#,Google Chrome,Selenium,Selenium Webdriver,我有一个相对简单的问题,但我似乎在任何地方都找不到答案。每当我在Selenium Chrome Web驱动程序打开的情况下关闭Windows窗体时,它都会关闭Windows窗体/应用程序,但保持Chrome控制台和浏览器处于打开状态。是否有任何方法可以使用driver.Quit()或.Close();表单何时关闭?类似我下面的例子 private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs

我有一个相对简单的问题,但我似乎在任何地方都找不到答案。每当我在Selenium Chrome Web驱动程序打开的情况下关闭Windows窗体时,它都会关闭Windows窗体/应用程序,但保持Chrome控制台和浏览器处于打开状态。是否有任何方法可以使用driver.Quit()或.Close();表单何时关闭?类似我下面的例子

    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        driver.Quit();
    }
尝试以下方法

driver.Quit();
driver = null;
尝试以下方法

driver.Quit();
driver = null;

问题是,当您按下关闭按钮时,关闭表单的事件不会被捕获

您必须订阅FormClosing事件并调用驱动程序上的Quit方法

    public Form1()
    {
        // (...)
        this.FormClosing += this.Form1_FormClosing;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        driver.Quit();
        Environment.Exit(0);
    }

请注意,当您使用F5运行应用程序时,驱动程序的控制台将关闭,但当您使用CTRL-F5运行应用程序时,驱动程序的控制台将不会关闭

问题在于,当您按下关闭按钮时,关闭表单的事件不会被捕获

您必须订阅FormClosing事件并调用驱动程序上的Quit方法

    public Form1()
    {
        // (...)
        this.FormClosing += this.Form1_FormClosing;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        driver.Quit();
        Environment.Exit(0);
    }
请注意,当您使用F5运行应用程序时,驱动程序的控制台将关闭,但当您使用CTRL-F5运行应用程序时,驱动程序的控制台将不会关闭