簡體   English   中英

Selenium.WebDriver.ChromeDriver 啟動緩慢 - 為什么?

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

我創建了最簡單的 NUnit 測試來初始化ChromeDriver ,它需要將近 6 秒才能運行。 大多數時間(大約 4 秒)都花在了空白的、非活動的 Chrome window 上。

空白 Chrome 窗口看起來像這樣

ChromeDriver.exe window 幾乎立即出現。

ChromeDriver.exe 窗口

我的單元測試如下所示:

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

我正在使用這些 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" />

我的問題是:

這種緩慢是預期的嗎? 我可以做些什么來加快速度嗎?

幾個月前,我(和其他人)構建了一個 web 機器人(不要問什么:-))。 目標表面是一個特定的網站。 為了准備,我們在離線局域網環境中盡可能多地復制了目標網站。 在其他工具中,我們使用 Selenium 運行 Chrome,啟動時間約為 4-6 秒,演出是 Intel Core i5 ~3 GHz 8GB RAM Windows 10 Pro 和另一台類似的計算機,筆記本電腦,主要區別在於運行 Ubuntu 18.04,大約相同的啟動時間。

C# 不是這里的問題。 在注意到延遲(至少對我來說不是什么大問題)之后,考慮到離線設置(沒有請求在互聯網上飛來飛去),以及不同的計算機構建,我敢說這是啟動時間,僅此而已,沒有猴子生意。

通過使用具有更快 memory、更快 CPU、更快總線、SSD 驅動器等的高端 gig 可以實現更快的啟動時間...

我正在使用以下 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();
    }
}

在 windows 自動代理設置中,可以關閉“自動檢測設置”,看看有沒有區別。 但是,我猜想,為ChromeOptions傳遞 null 代理具有類似的效果。

在此處輸入圖像描述

我已經得出結論,這只是需要多長時間,並且想出了一種方法來重用瀏覽器 windows。

如果您通過采用ChromeDriverService的重載啟動ChromeDriver並按如下方式設置EnableVerboseLogging ...

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

webDriver = new ChromeDriver(service, options);

... ChromeDriver window 在啟動時顯示詳細信息,包括(UNIX 樣式)時間戳。 我現在很滿意開始所花費的時間是正常的。

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"
}

Chrome 驅動程序加載速度非常慢的原因有多種。 我看到您已經找到了第一個代理設置。 第二個是驅動程序的位數。 我們體驗過 64 位版本的 ChromeDriver 非常慢。 您可以嘗試使用其他版本的 Chrome 驅動程序。

我們正在使用這個版本:

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

這與增加 Chrome 驅動程序的加載時間無關,而是您如何處理它; 當您執行以下代碼時,它會打開兩個可執行文件。 您的 ChromeDriver.exe 和 chrome.exe。

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

如果您在每個測試中沒有不同的 Chrome 選項,我強烈建議您為所有測試只初始化一次驅動程序並使用 Quit()、Close() 等管理您的 chrome.exe。請參閱 [設置]或 [OnetimeSetup] 屬性,具體取決於您的 NUnit 版本。 然后您的總測試運行執行時間將顯着減少。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM