【问题标题】:Selenium geckodriver executes findElement 10 times slower than chromedriver (.Net)Selenium geckodriver 执行 findElement 比 chromedriver (.Net) 慢 10 倍
【发布时间】:2019-05-06 20:37:52
【问题描述】:

抱歉,没有找到类似的问题,也许有人可以提供帮助。

由于额外的要求,我们不仅要使用 Chrome,还要使用 Firefox 来测试我们的项目。当我们简单地将测试上下文更改为 Firefox 时,结果发现 findElement 的所有调用所花费的时间是 Chrome 的 10 倍。所有的测试都被彻底破坏了。我们尝试使用不同的测试机器,但结果是一样的。该项目位于 Core .Net 上。对于测试,我们使用 MSTest V2、Firefox 63(64 位)和 Geckodriver 0.22(64 位)。

非常感谢任何帮助。

【问题讨论】:

  • 当客户端使用 http 连接到服务器时,会使用 http 标头进行协商以找到常见的操作模式。例如,如果服务器可以支持法语和英语,则标头确定要使用的语言。差异可能是由于使用了 http 1.0(流模式)而不是 http 1.1(块模式)。或者响应正在使用gzip 模式,数据被打包。所以我会使用像wireshark或fiddler这样的嗅探器并比较http标头以查看差异。通常向您的应用程序添加缺少的标头可以解决这些问题。
  • 您有任何数据/测试结果可以得出结论...findElement 花费的时间是 Chrome 的 10 倍...?

标签: c# selenium mstest geckodriver


【解决方案1】:

参考前面的回答,我的问题通过下面的代码解决了。

string geckoDriverDirectory = "Path of geckodriver.exe"
FirefoxDriverService geckoService = 
FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
geckoService.Host = "::1";
var firefoxOptions = new FirefoxOptions();
firefoxOptions.AcceptInsecureCertificates = true;
Driver = new FirefoxDriver(geckoService, firefoxOptions);

【讨论】:

  • 天哪。我只是复制粘贴它,它使我的代码快了 100 倍。另外作为旁注,如果 geckodriver 在您的路径中,您可以创建 geckoService 而无需指定 geckodriver 的目录。
  • 相同。将主机设置为"::1" 使 Firefox 与 Chrome 一样快。什么鬼,火狐。正如@MdNarimani 提到的,如果您不需要指定路径,您可以在 3 行中执行此操作:var svc = FirefoxDriverService.CreateDefaultService(); svc.Host = "::1"; return new FirefoxDriver(svc);
  • 设置 Host 属性时,我什么也做不了。无论我在那里输入什么字符串,即使它是 ::1,只会导致驱动程序出错:OpenQA.Selenium.WebDriverException:无法在localhost:51778 上启动驱动程序服务
【解决方案2】:

是的。您肯定遇到了 .NET Core 的性能问题。它不会在 Chrome、IE 或 Edge 上发生,因为每个浏览器的驱动程序可执行文件(与 geckodriver 不同)都侦听 IPv4 和 IPv6 环回地址。如果您将“::1”指定为 .NET 的 geckodriver 的主机,问题就会消失。

参考https://github.com/SeleniumHQ/selenium/issues/6597

【讨论】:

    【解决方案3】:

    适用于 Firefox 7/14/2020 的完整 .Net Core Web 驱动程序:

    // .Net Core workaround #1: Slow Firefox webdriver
    string projectFolder = Directory.GetParent(Directory.GetCurrentDirectory()).FullName;
    string geckoDriverDirectory = projectFolder + "\\netcoreapp3.1\\";
    FirefoxDriverService geckoService = 
    FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
    geckoService.Host = "::1";
    
    var ffOptions = new FirefoxOptions();
    ffOptions.BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\Firefox.exe"; 
    ffOptions.AcceptInsecureCertificates = true;
    
    // This profile will by-pass *ALL* credentials. Note that Chrome uses Internet Explorer settings, so it does not need this.
    // You must pre-setup the profile, by launching Firefox and doing phone authentication
    // The profile's location is: C:\Users\<windows logon>\AppData\Local\Mozilla\Firefox\Profiles
    // Without this, if your AUT uses SSO, you will always get prompted for the PIN or Two factor authentication
    FirefoxProfile profile = new FirefoxProfileManager().GetProfile("Selenium");
    ffOptions.Profile = profile;
    
    // DotNet Core workaround #2- Code page
    // https://stackoverflow.com/questions/56802715/firefoxwebdriver-no-data-is-available-for-encoding-437
    // https://stackoverflow.com/questions/50858209/system-notsupportedexception-no-data-is-available-for-encoding-1252
    CodePagesEncodingProvider.Instance.GetEncoding(437);
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    
    _driver = new FirefoxDriver(geckoService, ffOptions);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多