

下記のコードを実行するとエラーが発生します。

それぞれのオプションを個別に指定するとエラーになりません。  
`headless` と `user-data-dir` は同時に指定できないのでしょうか。

**実行したコード:**

[code]

    var driverDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
    var options = new ChromeOptions();
    options.AddArgument("--headless");
    options.AddArguments("user-data-dir=userdata");
    var driver = new ChromeDriver(driverDirectory, options);
    
[/code]

**エラーメッセージ:**

[code]

    OpenQA.Selenium.WebDriverException
    HResult=0x80131500
    Message=The HTTP request to the remote WebDriver server for URL http://localhost:63525/session timed out after 60 seconds.
    Source=WebDriver
    スタック トレース:
    at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
    at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
    at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
    at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
    at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
    at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
    
[/code]

**開発環境:**  
ChromeDriver 2.45.615291  
.NET Core 2.2 コンソールアプリケーション


CC BY-SA 4.0
2018-12-22T11:07:21.637
51441
2023-08-21T05:00:30.117
2022-03-22T01:26:41.297
3060
3925
question
c# selenium selenium-webdriver .net-core chromedriver
chromedriver で headless と user-data-dir のオプションを同時に指定するとエラーが発生する


**全面改訂**

Seleniumの版数が上がった(4.x系になった)せいなのか、両方指定してもOKになっていました。  
ただし、(Windowsでは？)ディレクトリの指定は絶対パスにしなければならないようです。

こちらの記事を参考に、TargetFrameworkをnet6.0でコンソールアプリを作成してみました。  
[【C#】Seleniumを使ってChromeを自動操作してみる](https://www.hanachiru-
blog.com/entry/2020/03/12/120000)  
上記のmain関数内を以下のように書き変えてビルドし確認しています。  
ディレクトリはあらかじめ作成しています。

[code]

    string DriverPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.AddArgument("--headless");
    chromeOptions.AddArguments("--user-data-dir=C:\\Develop\\ChromeUserData");
    
    IWebDriver driver = new ChromeDriver(DriverPath, chromeOptions);
    driver.Navigate().GoToUrl(@"https://www.google.co.jp/");// URLに移動します。
    
    Console.ReadKey();// なにかコンソールに文字を入力したらクロームを閉じる
    driver.Quit();
    
[/code]

* * *

なお、`chromeOptions.AddArguments("--user-data-
dir=C:\\Develop\\ChromeUserData");`の部分を`chromeOptions.AddArguments("--user-
data-
dir=ChromeUserData");`という風に相対パス指定すると、ディレクトリやいくつかのファイルは出来るのですが、以下のようなエラーになります。  
つまり相対パス指定だと`C:\Program Files
(x86)`の`Chrome`のインストール先にディレクトリやファイルを作ろうとしてエラーになっているようです。  
ただし指定した`ChromeUserData`ディレクトリは、ビルドして動作させているアプリケーションの.exeのあるディレクトリにも作られているのが不思議ですが。

[code]

    Starting ChromeDriver 99.0.4844.51 (d537ec02474b5afe23684e7963d538896c63ac77-refs/branch-heads/4844@{#875}) on port 65379
    Only local connections are allowed.
    Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
    ChromeDriver was started successfully.
    
    DevTools listening on ws://127.0.0.1:65382/devtools/browser/bccc2591-2ceb-4f7f-9b1a-9307e9796b8e
    [0322/100832.023:ERROR:devtools_http_handler.cc(291)] Error writing DevTools active port to file
    [0322/100832.025:ERROR:simple_backend_impl.cc(80)] Failed to create directory: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\js
    [0322/100832.025:ERROR:simple_backend_impl.cc(80)] Failed to create directory: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\wasm
    [0322/100832.027:ERROR:simple_backend_impl.cc(80)] Failed to create directory: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\js
    [0322/100832.027:ERROR:simple_backend_impl.cc(80)] Failed to create directory: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\wasm
    [0322/100832.027:ERROR:simple_backend_impl.cc(735)] Simple Cache Backend: wrong file structure on disk: 1 path: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\js
    [0322/100832.027:ERROR:simple_backend_impl.cc(735)] Simple Cache Backend: wrong file structure on disk: 1 path: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\wasm
    [0322/100832.028:ERROR:simple_backend_impl.cc(80)] Failed to create directory: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\js
    [0322/100832.029:ERROR:simple_backend_impl.cc(80)] Failed to create directory: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\wasm
    [0322/100832.030:ERROR:disk_cache.cc(185)] Unable to create cache
    [0322/100832.030:ERROR:simple_backend_impl.cc(80)] Failed to create directory: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\js
    [0322/100832.030:ERROR:simple_backend_impl.cc(80)] Failed to create directory: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\wasm
    [0322/100832.031:ERROR:shader_disk_cache.cc(612)] Shader Cache Creation failed: -2
    [0322/100832.031:ERROR:simple_backend_impl.cc(735)] Simple Cache Backend: wrong file structure on disk: 1 path: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\js
    [0322/100832.032:ERROR:simple_backend_impl.cc(735)] Simple Cache Backend: wrong file structure on disk: 1 path: C:\Program Files (x86)\Google\Chrome\Application\99.0.4844.74\ChromeUserData\Default\Code Cache\wasm
    [0322/100832.032:ERROR:disk_cache.cc(185)] Unable to create cache
    [0322/100832.033:ERROR:disk_cache.cc(185)] Unable to create cache
    Unhandled exception. OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:65379/session timed out after 60 seconds.
     ---> System.Threading.Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.
     ---> System.TimeoutException: The operation was canceled.
     ---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
     ---> System.IO.IOException: Unable to read data from the transport connection: スレッドの終了またはアプリケーションの要求によって、I/O 処理は中止されました。.
     ---> System.Net.Sockets.SocketException (995): スレッドの終了またはアプリケーションの要求によって、I/O 処理は中止されました。
       --- End of inner exception stack trace ---
       at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
       at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource<System.Int32>.GetResult(Int16 token)
       at System.Net.Http.HttpConnection.InitialFillAsync(Boolean async)
       at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
       --- End of inner exception stack trace ---
       at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
       at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
       at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
       at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
       --- End of inner exception stack trace ---
       --- End of inner exception stack trace ---
       at System.Net.Http.HttpClient.HandleFailure(Exception e, Boolean telemetryStarted, HttpResponseMessage response, CancellationTokenSource cts, CancellationToken cancellationToken, CancellationTokenSource pendingRequestsCts)
       at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
       at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
       at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
       --- End of inner exception stack trace ---
       at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
       at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
       at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
       at OpenQA.Selenium.WebDriver.StartSession(ICapabilities desiredCapabilities)
       at OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities)
       at OpenQA.Selenium.Chromium.ChromiumDriver..ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
       at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
       at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout)
       at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options)
       at SeleniumTest.MainClass.Main(String[] args) in C:\Develop\CSharpSelenium\CSharpSelenium\Program.cs:line 34
    
[/code]


CC BY-SA 4.0
2018-12-22T13:16:53.173
51445
2022-03-22T01:23:15.550
2022-03-22T01:23:15.550
26370
26370
51441
answer