简体   繁体   English

Selenium.WebDriver.ChromeDriver 启动缓慢 - 为什么?

[英]Selenium.WebDriver.ChromeDriver slow to launch - why?

I've created the simplest-possible NUnit test to initialise a ChromeDriver , and it's taking nearly 6 seconds to run.我创建了最简单的 NUnit 测试来初始化ChromeDriver ,它需要将近 6 秒才能运行。 Most of the time (roughly 4s) is spent with a blank, inactive Chrome window.大多数时间(大约 4 秒)都花在了空白的、非活动的 Chrome window 上。

空白 Chrome 窗口看起来像这样

The ChromeDriver.exe window appears almost instantly. ChromeDriver.exe window 几乎立即出现。

ChromeDriver.exe 窗口

My unit test looks like this:我的单元测试如下所示:

[Test]
public void Simplest_Possible_Test()
{
    var options = new ChromeOptions { Proxy = null };
    using (new ChromeDriver(options))
    {
        // Do nothing
    }
}

and I'm using these nuget packages:我正在使用这些 nuget 包:

  <package id="NUnit" version="3.12.0" targetFramework="net47" />
  <package id="Selenium.Support" version="3.141.0" targetFramework="net47" />
  <package id="Selenium.WebDriver" version="3.141.0" targetFramework="net47" />
  <package id="Selenium.WebDriver.ChromeDriver" version="78.0.3904.7000" targetFramework="net47" />

My question is:我的问题是:

Is this slowness expected?这种缓慢是预期的吗? Can I do anything to speed it up?我可以做些什么来加快速度吗?

A few months ago, I (and others), built a web bot (don't ask what for:-) ).几个月前,我(和其他人)构建了一个 web 机器人(不要问什么:-))。 The target surface was a particular website.目标表面是一个特定的网站。 For preparations, we replicated as much as possible the target website in an offline LAN environment.为了准备,我们在离线局域网环境中尽可能多地复制了目标网站。 Among other tools, we used Selenium to run Chrome, and startup time was ~4-6 seconds, the gig was a Intel Core i5 ~3 GHz 8GB RAM Windows 10 Pro and had another similar computer, a laptop, main difference being it was running Ubuntu 18.04, about same startup time.在其他工具中,我们使用 Selenium 运行 Chrome,启动时间约为 4-6 秒,演出是 Intel Core i5 ~3 GHz 8GB RAM Windows 10 Pro 和另一台类似的计算机,笔记本电脑,主要区别在于运行 Ubuntu 18.04,大约相同的启动时间。

C# is not the issue here. C# 不是这里的问题。 After noticing that delay (which is not a big deal, at least for me), considering the offline setting (no requests flying around the Internet), and the different computer builds, I dare to say it's the startup time it takes, that's all, no monkey business.在注意到延迟(至少对我来说不是什么大问题)之后,考虑到离线设置(没有请求在互联网上飞来飞去),以及不同的计算机构建,我敢说这是启动时间,仅此而已,没有猴子生意。

Faster startup times could be achieved by using a higher end gig with faster memory, faster CPU, faster buses, SSD drives, etc...通过使用具有更快 memory、更快 CPU、更快总线、SSD 驱动器等的高端 gig 可以实现更快的启动时间...

I am using following singleton IWebDriver instance and it only takes couple of seconds to launch.我正在使用以下 singleton IWebDriver 实例,启动只需几秒钟。

public class UiTest : IDisposable
{
    private IWebDriver driver = null;

    protected IWebDriver Driver
    {
        get
        {
            if (driver == null)
            {
                driver = new ChromeDriver(new ChromeOptions{Proxy = null});
                driver.Manage().Window.Maximize();
            }
            return _driver;
        }
    }

    public void Dispose()
    {
        driver?.Dispose();
    }
}

In windows Automatic proxy setup, you can turn off "Automatic detect settings" and see if there is any difference.在 windows 自动代理设置中,可以关闭“自动检测设置”,看看有没有区别。 However, passing null proxy for ChromeOptions has the similar effect, I guess.但是,我猜想,为ChromeOptions传递 null 代理具有类似的效果。

在此处输入图像描述

I have concluded that this is just how long it takes, and have come up with a way to re-use browser windows.我已经得出结论,这只是需要多长时间,并且想出了一种方法来重用浏览器 windows。

If you start the ChromeDriver via the overload that takes a ChromeDriverService and set EnableVerboseLogging as follows...如果您通过采用ChromeDriverService的重载启动ChromeDriver并按如下方式设置EnableVerboseLogging ...

var service = ChromeDriverService.CreateDefaultService();
service.EnableVerboseLogging = true;

webDriver = new ChromeDriver(service, options);

... the ChromeDriver window shows verbose info on start-up, including (UNIX-style) timestamps. ... ChromeDriver window 在启动时显示详细信息,包括(UNIX 样式)时间戳。 I'm now satisfied that the time taken to start is simply normal.我现在很满意开始所花费的时间是正常的。

Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 53677
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1573036834.332][INFO]: [dd05361c2fafe521ffd5fb06e6dbe7a3] COMMAND InitSession {
   "capabilities": {
      "firstMatch": [ {
         "browserName": "chrome",
         "goog:chromeOptions": {
            "args": [ "--profile-directory=Default-16031ab478b34fa8838ecc0cb76b2cd1", "--user-data-dir=C:\\Users\\xxx\\AppData\\Local\\Temp\\9f19589865c44ba6aec806fbafc1b561" ]
         }
      } ]
   },
   "desiredCapabilities": {
      "browserName": "chrome",
      "goog:chromeOptions": {
         "args": [ "--profile-directory=Default-16031ab478b34fa8838ecc0cb76b2cd1", "--user-data-dir=C:\\Users\\xxx\\AppData\\Local\\Temp\\9f19589865c44ba6aec806fbafc1b561" ]
      }
   }
}
[1573036834.335][INFO]: Populating Preferences file: {
   "alternate_error_pages": {
      "enabled": false
   },
   "autofill": {
      "enabled": false
   },
   "browser": {
      "check_default_browser": false
   },
   "distribution": {
      "import_bookmarks": false,
      "import_history": false,
      "import_search_engine": false,
      "make_chrome_default_for_user": false,
      "skip_first_run_ui": true
   },
   "dns_prefetching": {
      "enabled": false
   },
   "profile": {
      "content_settings": {
         "pattern_pairs": {
            "https://*,*": {
               "media-stream": {
                  "audio": "Default",
                  "video": "Default"
               }
            }
         }
      },
      "default_content_setting_values": {
         "geolocation": 1
      },
      "default_content_settings": {
         "geolocation": 1,
         "mouselock": 1,
         "notifications": 1,
         "popups": 1,
         "ppapi-broker": 1
      },
      "password_manager_enabled": false
   },
   "safebrowsing": {
      "enabled": false
   },
   "search": {
      "suggest_enabled": false
   },
   "translate": {
      "enabled": false
   }
}
[1573036834.337][INFO]: Populating Local State file: {
   "background_mode": {
      "enabled": false
   },
   "ssl": {
      "rev_checking": {
         "enabled": false
      }
   }
}
[1573036834.348][INFO]: Cannot switch to US keyboard layout - some keys may be interpreted incorrectly
[1573036834.348][INFO]: Launching chrome: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --force-fieldtrials --ignore-certificate-errors --load-extension="C:\Users\PAUL~1.SUA\AppData\Local\Temp\scoped_dir18488_412533912\internal" --log-level=0 --no-first-run --password-store=basic --profile-directory=Default-16031ab478b34fa8838ecc0cb76b2cd1 --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir="C:\Users\xxx\AppData\Local\Temp\9f19589865c44ba6aec806fbafc1b561"

DevTools listening on ws://127.0.0.1:53680/devtools/browser/bacb830a-c373-460e-83e8-10ab7f05d6ce
[1573036834.744][DEBUG]: DevTools HTTP Request: http://localhost:53680/json/version
[1573036835.088][DEBUG]: DevTools HTTP Response: {
   "Browser": "Chrome/78.0.3904.70",
   "Protocol-Version": "1.3",
   "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
   "V8-Version": "7.8.279.17",
   "WebKit-Version": "537.36 (@edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5)",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/browser/bacb830a-c373-460e-83e8-10ab7f05d6ce"
}

[1573036835.104][DEBUG]: DevTools HTTP Request: http://localhost:53680/json
[1573036835.122][DEBUG]: DevTools HTTP Response: [ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:53680/devtools/page/C5722CC4576C0C53221985793E2EB260",
   "id": "C5722CC4576C0C53221985793E2EB260",
   "title": "Chrome Automation Extension",
   "type": "background_page",
   "url": "chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/page/C5722CC4576C0C53221985793E2EB260"
}, {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:53680/devtools/page/4B8C0B4FB68B7222E5E34BB28D75A6E4",
   "id": "4B8C0B4FB68B7222E5E34BB28D75A6E4",
   "title": "New Tab",
   "type": "page",
   "url": "chrome://newtab/",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/page/4B8C0B4FB68B7222E5E34BB28D75A6E4"
} ]

[1573036835.131][DEBUG]: DevTools HTTP Request: http://localhost:53680/json
[1573036835.135][DEBUG]: DevTools HTTP Response: [ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:53680/devtools/page/C5722CC4576C0C53221985793E2EB260",
   "id": "C5722CC4576C0C53221985793E2EB260",
   "title": "Chrome Automation Extension",
   "type": "background_page",
   "url": "chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/page/C5722CC4576C0C53221985793E2EB260"
}, {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:53680/devtools/page/4B8C0B4FB68B7222E5E34BB28D75A6E4",
   "id": "4B8C0B4FB68B7222E5E34BB28D75A6E4",
   "title": "New Tab",
   "type": "page",
   "url": "chrome://newtab/",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/page/4B8C0B4FB68B7222E5E34BB28D75A6E4"
} ]

[1573036835.152][INFO]: resolved localhost to ["::1","127.0.0.1"]
[1573036837.137][WARNING]: Timed out connecting to Chrome, retrying...
[1573036837.138][INFO]: resolved localhost to ["::1","127.0.0.1"]
[1573036839.141][DEBUG]: DevTools WebSocket Command: Log.enable (id=1) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.142][DEBUG]: DevTools WebSocket Command: DOM.getDocument (id=2) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.144][DEBUG]: DevTools WebSocket Command: Target.setAutoAttach (id=3) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "autoAttach": true,
   "waitForDebuggerOnStart": false
}
[1573036839.145][DEBUG]: DevTools WebSocket Command: Page.enable (id=4) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.146][DEBUG]: DevTools WebSocket Command: Page.enable (id=5) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.146][DEBUG]: DevTools WebSocket Response: Log.enable (id=1) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.147][DEBUG]: DevTools WebSocket Response: DOM.getDocument (id=2) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "root": {
      "backendNodeId": 2,
      "baseURL": "chrome-search://local-ntp/local-ntp.html",
      "childNodeCount": 2,
      "children": [ {
         "backendNodeId": 14,
         "localName": "",
         "nodeId": 2,
         "nodeName": "html",
         "nodeType": 10,
         "nodeValue": "",
         "parentId": 1,
         "publicId": "",
         "systemId": ""
      }, {
         "attributes": [ "lang", "en", "class", "inited" ],
         "backendNodeId": 15,
         "childNodeCount": 4,
         "children": [ {
            "backendNodeId": 16,
            "localName": "",
            "nodeId": 4,
            "nodeName": "#comment",
            "nodeType": 8,
            "nodeValue": " TODO(dbeam): dir=\"ltr\"? ",
            "parentId": 3
         }, {
            "backendNodeId": 17,
            "localName": "",
            "nodeId": 5,
            "nodeName": "#comment",
            "nodeType": 8,
            "nodeValue": " Copyright 2015 The Chromium Authors. All rights reserved.\n     Use of this source code is governed by a BSD-style license that can be\n     found in the LICENSE file. ",
            "parentId": 3
         }, {
            "attributes": [  ],
            "backendNodeId": 18,
            "childNodeCount": 24,
            "localName": "head",
            "nodeId": 6,
            "nodeName": "HEAD",
            "nodeType": 1,
            "nodeValue": "",
            "parentId": 3
         }, {
            "attributes": [ "class", "light-chip win", "style", "background: rgb(255, 255, 255); --logo-color:rgba(238,238,238,1);" ],
            "backendNodeId": 19,
            "childNodeCount": 21,
            "localName": "body",
            "nodeId": 7,
            "nodeName": "BODY",
            "nodeType": 1,
            "nodeValue": "",
            "parentId": 3
         } ],
         "frameId": "4B8C0B4FB68B7222E5E34BB28D75A6E4",
         "localName": "html",
         "nodeId": 3,
         "nodeName": "HTML",
         "nodeType": 1,
         "nodeValue": "",
         "parentId": 1
      } ],
      "documentURL": "chrome-search://local-ntp/local-ntp.html",
      "localName": "",
      "nodeId": 1,
      "nodeName": "#document",
      "nodeType": 9,
      "nodeValue": "",
      "xmlVersion": ""
   }
}
[1573036839.148][DEBUG]: DevTools WebSocket Response: Target.setAutoAttach (id=3) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.148][DEBUG]: DevTools WebSocket Response: Page.enable (id=4) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.148][DEBUG]: DevTools WebSocket Response: Page.enable (id=5) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.149][DEBUG]: DevTools WebSocket Command: Runtime.enable (id=6) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.150][DEBUG]: DevTools WebSocket Event: Runtime.executionContextCreated 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "context": {
      "auxData": {
         "frameId": "04C2466A16DFD233FEE7E6F1A4D5B59C",
         "isDefault": true,
         "type": "default"
      },
      "id": 4,
      "name": "",
      "origin": "chrome-search://most-visited"
   }
}
[1573036839.150][DEBUG]: DevTools WebSocket Event: Runtime.executionContextCreated 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "context": {
      "auxData": {
         "frameId": "B6EA84A25C9E0EFFB88946CC7606D044",
         "isDefault": true,
         "type": "default"
      },
      "id": 3,
      "name": "",
      "origin": "chrome-search://most-visited"
   }
}
[1573036839.151][DEBUG]: DevTools WebSocket Event: Runtime.executionContextCreated 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "context": {
      "auxData": {
         "frameId": "BA8BAD5C705B50773F7EC3B2596AE5B7",
         "isDefault": true,
         "type": "default"
      },
      "id": 2,
      "name": "",
      "origin": "chrome-search://local-ntp"
   }
}
[1573036839.151][DEBUG]: DevTools WebSocket Event: Runtime.executionContextCreated 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "context": {
      "auxData": {
         "frameId": "4B8C0B4FB68B7222E5E34BB28D75A6E4",
         "isDefault": true,
         "type": "default"
      },
      "id": 1,
      "name": "",
      "origin": "chrome-search://local-ntp"
   }
}
[1573036839.151][DEBUG]: DevTools WebSocket Response: Runtime.enable (id=6) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.154][DEBUG]: DevTools WebSocket Command: Page.enable (id=7) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.155][DEBUG]: DevTools WebSocket Response: Page.enable (id=7) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.155][DEBUG]: DevTools WebSocket Command: Runtime.enable (id=8) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.156][DEBUG]: DevTools WebSocket Response: Runtime.enable (id=8) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.157][DEBUG]: DevTools WebSocket Command: Runtime.evaluate (id=9) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "awaitPromise": true,
   "expression": "(function() { // Copyright (c) 2012 The Chromium Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style license that can be\n// found in the LICENSE file.\n\n/**\n * Enum f...",
   "returnByValue": true
}
[1573036839.159][DEBUG]: DevTools WebSocket Response: Runtime.evaluate (id=9) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "result": {
      "type": "object",
      "value": {
         "status": 0,
         "value": 1
      }
   }
}
[1573036839.160][INFO]: [dd05361c2fafe521ffd5fb06e6dbe7a3] RESPONSE InitSession {
   "capabilities": {
      "acceptInsecureCerts": false,
      "browserName": "chrome",
      "browserVersion": "78.0.3904.70",
      "chrome": {
         "chromedriverVersion": "78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800})",
         "userDataDir": "C:\\Users\\xxx\\AppData\\Local\\Temp\\9f19589865c44ba6aec806fbafc1b561"
      },
      "goog:chromeOptions": {
         "debuggerAddress": "localhost:53680"
      },
      "networkConnectionEnabled": false,
      "pageLoadStrategy": "normal",
      "platformName": "windows nt",
      "proxy": {

      },
      "setWindowRect": true,
      "strictFileInteractability": false,
      "timeouts": {
         "implicit": 0,
         "pageLoad": 300000,
         "script": 30000
      },
      "unhandledPromptBehavior": "dismiss and notify"
   },
   "sessionId": "dd05361c2fafe521ffd5fb06e6dbe7a3"
}

There are multiple reasons where Chrome driver is loading very slow. Chrome 驱动程序加载速度非常慢的原因有多种。 I see you've already found the first one which is the proxy setting.我看到您已经找到了第一个代理设置。 The second one is the bitness of the driver.第二个是驱动程序的位数。 We've experienced 64-bit version of ChromeDriver was very slow.我们体验过 64 位版本的 ChromeDriver 非常慢。 You can try using other versions of the Chrome Driver.您可以尝试使用其他版本的 Chrome 驱动程序。

We are using this version:我们正在使用这个版本:

<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="2.45.0" />

This is not about increasing the load time of the Chrome Driver but how you approach it;这与增加 Chrome 驱动程序的加载时间无关,而是您如何处理它; When you execute the following piece of code, it opens two executables.当您执行以下代码时,它会打开两个可执行文件。 Your ChromeDriver.exe and chrome.exe.您的 ChromeDriver.exe 和 chrome.exe。

    var options = new ChromeOptions { Proxy = null };
    using (new ChromeDriver(options))
    {
        // Do nothing
    }

If you are don't have different Chrome options for each test, I strongly recommend you to initialize your driver for only once for your all tests and manage your chrome.exe with Quit(), Close() etc. Please see [Setup] or [OnetimeSetup] attributes depending on your version of NUnit.如果您在每个测试中没有不同的 Chrome 选项,我强烈建议您为所有测试只初始化一次驱动程序并使用 Quit()、Close() 等管理您的 chrome.exe。请参阅 [设置]或 [OnetimeSetup] 属性,具体取决于您的 NUnit 版本。 Then your total test run execution time will decrease significantly.然后您的总测试运行执行时间将显着减少。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebDriver.ChromeDriver.win32 与 Selenium.WebDriver.ChromeDriver - WebDriver.ChromeDriver.win32 vs Selenium.WebDriver.ChromeDriver 在 Selenium.WebDriver.ChromeDriver NuGet package 中找不到更新的 Chrome 浏览器 - Updated Chrome Browser not found in Selenium.WebDriver.ChromeDriver NuGet package Selenium.WebDriver.ChromeDriver - chromedriver.exe没有为netcore2.2目标框架发布 - Selenium.WebDriver.ChromeDriver - chromedriver.exe is not being publishing for netcore2.2 target framework Selenium Chromedriver在发布时不会上网 - Selenium Chromedriver not going to url on launch Selenium WebDriver C# 测试脚本中不存在 ChromeDriver - ChromeDriver does not exist in Selenium WebDriver C# test script 安装了Selenium WebDriver.ChromeDriver Nuget包,但不适用于MSTest - Selenium WebDriver.ChromeDriver Nuget package installed, but not working for MSTest 在Visual Studio中使用C#和ChromeDriver进行MS Test的Selenium WebDriver - Selenium WebDriver with MS Test using C# and ChromeDriver in Visual Studio 在哪里放chromedriver.exe以发布Selenium WebDriver C# - Where to put chromedriver.exe for release of Selenium WebDriver C# Selenium Webdriver(经过测试的ChromeDriver)无法通过包含变音符号的文本找到选项? - Selenium webdriver (tested ChromeDriver) cant find option by text containing umlauts? Selenium WebDriver C#完整网站截图使用ChromeDriver和FirefoxDriver - Selenium WebDriver C# Full Website Screenshots With ChromeDriver and FirefoxDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM