欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c# 从内存中释放Selenium chromedriver.exe

时间:2021-06-08 07:47:55|栏目:.NET代码|点击:

背景

我设置了一个c#代码来运行Selenium chromedriver.exe.在运行结束时,我有browser.close()来关闭实例。(browser = webdriver.Chrome())我相信它应该从内存中释放chromedriver.exe(我在Windows 7上)。但是每次运行后,内存中仍有一个chromedriver.exe实例。

问题窥探

从理论上讲,调用browser.Quit将关闭所有浏览器选项卡并终止进程。

但是,在我的情况下,我无法做到这一点 - 因为我并行运行多个测试,我不想进行一次测试来关闭其他人的窗口。因此,当我的测试完成运行时,仍有许多“chromedriver.exe”进程在运行。

解决办法

public override void DoJob(IJobExecutionContext context, ILifetimeScope scope, string[] args)
    {
      Console.WriteLine(nameof(LoginReptiles1688Job) + " 开始-------------------");
      ChromeOptions options = null;
      IWebDriver driver = null;
      try
      {
        options = new ChromeOptions();
        options.AddArguments("--ignore-certificate-errors");
        options.AddArguments("--ignore-ssl-errors");
        var listCookie = CookieHelp.GetCookie();
        if (listCookie != null)
        {
          // options.AddArgument("headless");
        }
        ChromeDriverService service = ChromeDriverService.CreateDefaultService(System.Environment.CurrentDirectory);
        service.HideCommandPromptWindow = true;
        driver = new ChromeDriver(service, options, TimeSpan.FromSeconds(120));
        var setLoginStatus = scope.Resolve<ISetLoginStatus>();
        IReptilesImageSearchService _reptilesImageSearchService = scope.Resolve<IReptilesImageSearchService>();
        CrawlingWeb(_reptilesImageSearchService, driver);
        CrawlingWebShop(_reptilesImageSearchService, driver);
      }
      catch (Exception ex)
      {
        throw ex;
      }
      finally
      {
        driver?.Close(); // Close the chrome window
        driver?.Quit(); // Close the console app that was used to kick off the chrome window
        driver?.Dispose(); // Close the chromedriver.exe

        driver = null;
        options = null;
        detailtry = 0;
        shoptry = 0;
        Console.WriteLine(nameof(LoginReptiles1688Job) + " 结束-------------------");
      }
    }

在C#控制台应用程序中使用了chrome驱动程序,只有在将所有三种方法一起调用后才能清理延迟进程。

上一篇:JS实现完美include加载功能代码

栏    目:.NET代码

下一篇:C#实现JSON解析器MojoUnityJson功能(简单且高效)

本文标题:c# 从内存中释放Selenium chromedriver.exe

本文地址:http://www.codeinn.net/misctech/138065.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有