【问题标题】:How to automatically close selenium chromedriver when Windows Form application closes or stops responding?Windows Form应用程序关闭或停止响应时如何自动关闭selenium chromedriver?
【发布时间】:2020-04-01 17:56:30
【问题描述】:

我想让 selenium chromedriver 在我的 Windows 窗体或程序关闭或停止响应时自动关闭。 chromedriver.exe 应该依赖于 Windows 窗体应用程序。

我正在使用 C# Windows 窗体应用程序 使用 Selenium chromedriver 自动执行浏览任务。我已经发现了一个与此类似的问题,除了我不仅希望 webdriver 在表单关闭事件时关闭,而且还希望在程序由于错误或其他原因而失败时关闭。

在我的例子中,我隐藏了 chromedriver.exe 的控制台窗口,所以当程序失败时,进程中隐藏了一个僵尸进程,我必须从任务管理器中手动杀死它。我在我的代码中使用了这一行:

chromeDriverService.HideCommandPromptWindow = true;

【问题讨论】:

    标签: c# selenium automation selenium-chromedriver


    【解决方案1】:

    我在使用 Firefox Selenium 浏览器时遇到了类似的问题。在您的 form.cs 中,您可以像这样覆盖 OnFormClosing 方法:

     protected override void OnFormClosing(FormClosingEventArgs e)
     {
         base.OnFormClosing(e);
         CloseBrowser();
     }
    

    CloseBrowser 方法:

     public void CloseBrowser()
     {
         driver.Close();
         driver.Dispose();
         driver.Quit();
     }
    

    这是针对 Firefox 的,但我相当肯定这段代码也适用于 Chrome 驱动程序!至于在程序没有响应时关闭,您可以将相同的 CloseBrowser 放在 Try/Catch 的 Catch 子句中,这意味着如果发生异常,浏览器将关闭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多