C# Selenium ChromeDriver: Google Chrome fails when run as administrator

4

I have encountered an usual situation when using Selenium Web Driver C# with Chrome: if the process running the tests is "run as administrator" (Visual Studio or nunit3-console.exe) Chrome will fail to load.

Context

  • OS: Windows 7 x64
  • Chrome: Version 64.0.3282.167 (Official Build) (64-bit)
  • Selenium.Chrome.WebDriver (chromedriver.exe): 2.35.0
  • Local policy does not allow chrome extensions

Steps

  1. Start a test that also initializes the driver

    var options = new ChromeOptions();
    
    //TODO: check if really needed
    options.AddAdditionalCapability("useAutomationExtension", false);
    options.AddArguments("--allow-no-sandbox-job");
    options.AddArguments("--ignore-certificate-errors");
    
    var driver = new ChromeDriver(options);
    
  2. ChromeDriver starts successfully:

    Starting ChromeDriver 2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73) on
    port 61771
    Only local connections are allowed.
    
    DevTools listening on ws://127.0.0.1:12890/devtools/browser/e50864bd-9c30-445c-a3f8-e33d6b6e5b49
    
  3. Chrome is opened, but the tab fails to load

chrome failed tab

  1. If I refresh the tab when being attached, I receive the following exception information:

An exception of type 'System.InvalidOperationException' occurred in WebDriver.dll but was not handled in user code

Additional information: session not created exception

from tab crashed

(Session info: chrome=64.0.3282.167)

(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64) (InsecureCertificate)

I can't figure why it is failing. I feel that it might be connected this issue, but the issue deals with another user, not the same user in elevated mode.

Question: Why C# Selenium ChromeDriver + Chrome fail when run as administrator. How can I investigate the root cause for this?


I have created a small Console Application as suggested in the comments:

static void Main(string[] args)
{
    var options = new ChromeOptions();
    // this is required since otherwise it will try to load some extension which is not allowed by local policy
    options.AddAdditionalCapability("useAutomationExtension", false);
    // try to catch some errors, but does not seem to work
    options.AddArguments("--enable-logging");
    options.AddArguments("--v=1");
    var driver = new ChromeDriver(options);
    driver.Navigate().GoToUrl("http://localhost");
}

Did some testing on another computer outside of the company and it works correctly regardless of Chrome Driver (tried both 2.35 and 2.36). Other difference would be the operating system (Windows 7 at work and Windows 10 at home) and the local policy at work (which might affect Chrome settings).


Thanks to Tarun Lalwani I reduced the problem to what seems to be related to Run as admin + local policy + possible Chrome issue:

  1. running Web driver in verbose mode indicated that Chrome uses a completely different profile path when run normally vs. run as admin:

    • Run normally: dir="C:\Users\<my-dos-profile-name>\AppData\Local\Temp\scoped_dir21000_25907" (changes for each run)
    • Run as admin: dir="C:\WINDOWS\TEMP\scoped_dir7236_26307"
  2. Inspecting chrome.debug file within Chrome's installation folder (there is also a debug file created within each temporary profile) I see these errors:

[0303/120050.213:ERROR:process_reader_win.cc(151)] SuspendThread: Access is denied. (0x5)
[0303/120050.214:ERROR:process_reader_win.cc(123)] NtOpenThread: {Access Denied} A process has requested access to an object, but has not been granted those access rights. (0xc0000022)
[0303/120050.215:ERROR:exception_snapshot_win.cc(88)] thread ID 13672 not found in process
[0303/120050.215:WARNING:crash_report_exception_handler.cc(62)] ProcessSnapshotWin::Initialize failed

  1. If I simply run Chrome as admin, all restore tabs are crashing and I obtain similar errors in the log. Console (cmd) output says:

C:\Program Files (x86)\Google\Chrome\Application>[7244:16744:0303/125934.383:ERROR:gpu_process_transport_factory.cc(1009)] Lost UI shared context.
[7244:13716:0303/125956.057:ERROR:connection_factory_impl.cc(381)] Failed to connect to MCS endpoint with error -118

So, at least now I know it is related to Chrome starting as admin and not related to Chrome driver.

google-chrome
selenium
selenium-webdriver
selenium-chromedriver
asked on Stack Overflow Feb 15, 2018 by Alexei - check Codidact • edited Mar 3, 2018 by Alexei - check Codidact

1 Answer

5

I managed to bypass this issue using --no-sandbox options. While it works in my case, I am not happy with the solution since no-sandbox is not a recommended option.

My final minimal working code:

var options = new ChromeOptions();
options.SetLoggingPreference(LogType.Driver, LogLevel.All);
options.AddAdditionalCapability("useAutomationExtension", false);
options.AddArguments("--no-sandbox");
var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("http://localhost");

SetLoggingPreference is not part of the solution, but it provided helpful in trying no-sandbox option. Web driver threw the following error when Chrome tab crashed:

[17532:18272:0301/225923.296:ERROR:gpu_process_transport_factory.cc(1009)] Lost UI shared context.


After digging more I found a simple and decent solution: start chromedriver.exe without elevation and connect to it using RemoteWebDriver instead of ChromeDriver (thanks to Tarun Lalwani for pointing out this pattern):

var options = new ChromeOptions();
options.SetLoggingPreference(LogType.Driver, LogLevel.All);
options.AddAdditionalCapability("useAutomationExtension", false);
var driver = new RemoteWebDriver(new Uri("http://localhost:9515"), options);
driver.Navigate().GoToUrl("http://localhost");
answered on Stack Overflow Mar 1, 2018 by Alexei - check Codidact • edited Mar 5, 2018 by Alexei - check Codidact

User contributions licensed under CC BY-SA 3.0