selenium: [πŸ› Bug]: IWebDriver.Manage().Network.StartMonitoring() causes file uploads to fail

What happened?

I have a project that uploads files into the web app under test on the Chrome browser. With Selenium 4.23 this was working flawlessly. I tried to update to Selenium 4.27 and with no changes other than this NuGet package version this stopped working.

The implementation of the web app under test is such that when files are uploaded this is done in 4MB chunks. If the file size is less than 4MB then the upload still works as it used to with 4.23. However if the size is greater than 4MB such that it is chunked then the upload will not work anymore.

Narrowing it down I eventually find that it is a call to IWebDriver.Manage().Network.StartMonitoring() that appears to be the cause. If I remove this line it works fine, with it it will not do the upload.

How can we reproduce the issue?

Here is a simple example to see the issue. Note that this is not the site I was testing, I cannot give you access to that, but this one appears to show the issue too. Note here I don’t know the chunking size for this site but I was observing failure using a file that was 80MB and seeing success with a file that was 1KB.


IWebDriver driver = null;
try
{
    driver = new ChromeDriver();
    driver.Manage().Network.StartMonitoring().GetAwaiter().GetResult();
    driver.Navigate().GoToUrl("https://tus.io/demo");
    Thread.Sleep(1000);
    driver.FindElement(By.Id("P0-0")).SendKeys("C:\\Users\\YourName\\file.file");
    Thread.Sleep(22000);
}
finally
{
    driver?.Quit();
}

Relevant log output

Trace level driver logs are 200MB so cannot upload or paste in here. Please download from: https://www.filemail.com/d/xbkqrdjzsxgdvms

Operating System

Windows 10

Selenium version

4.27.0

What are the browser(s) and version(s) where you see this issue?

Chrome 131.0.6778.140

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 131.0.6778.140

Are you using Selenium Grid?

I was but seems not necessary to reproduce the issue

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 20 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I caught it:

21:20:15.221 TRACE DevToolsSession: CDP RCV << {"method":"Fetch.requestPaused","params":{"requestId":"interception-job-27.0","request":{"url":"https://tusd.tusdemo.net/files/d98a9ddd6c3ff97e11702ac5de5834fa+jWEe0kWhtgB76yK8H8sOMJTKe8YkgD.YdEsgHbGinT8yu5T4_y06otqVdQ4CpcYu9naKLFXjZJrPceKYXORvQsNjy6Jh1HQLR9JabneigVwYIzvDJdmKVtesHSxTNFZH","method":"PATCH","headers":{"Accept":"*/*","Content-Type":"application/offset+octet-stream","Origin":"https://tus.io","Referer":"https://tus.io/","Tus-Resumable":"1.0.0","Upload-Offset":"0","User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36","X-Request-ID":"65500868-cb54-4da1-91ea-8b20d48e15db","sec-ch-ua":"\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"","sec-ch-ua-mobile":"?0","sec-ch-ua-platform":"\"Windows\""},"postData":"MZ...truncated...300KB...sBhlSvzaYd6AA=="}],"initialPriority":"High","referrerPolicy":"strict-origin-when-cross-origin"},"frameId":"89D7C96F5D38D5A7D3B241D276D0F2C7","resourceType":"XHR","networkId":"28664.36"},"sessionId":"4BA6A1A6EAF683025962B3925A89C493"}
21:20:15.242 ERROR DevToolsSession: Unexpected error occured while processing message: System.InvalidOperationException: Cannot read incomplete UTF-16 JSON text as string with missing low surrogate.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ReadIncompleteUTF16()
   at System.Text.Json.JsonReaderHelper.TryUnescape(ReadOnlySpan`1 source, Span`1 destination, Int32 idx, Int32& written)
   at System.Text.Json.JsonReaderHelper.Unescape(ReadOnlySpan`1 source, Span`1 destination, Int32& written)
   at System.Text.Json.JsonDocument.WriteString(DbRow& row, Utf8JsonWriter writer)
   at System.Text.Json.JsonDocument.WriteComplexElement(Int32 index, Utf8JsonWriter writer)
   at System.Text.Json.JsonDocument.WriteElementTo(Int32 index, Utf8JsonWriter writer)
   at System.Text.Json.Nodes.JsonObject.WriteTo(Utf8JsonWriter writer, JsonSerializerOptions options)
   at System.Text.Json.Nodes.JsonNode.WriteToPooledBuffer(JsonSerializerOptions options, JsonWriterOptions writerOptions, Int32 bufferSize)
   at System.Text.Json.Nodes.JsonNode.ToString()
   at OpenQA.Selenium.DevTools.DevToolsSession.ProcessMessage(String message) in D:\SeleniumHQ\selenium\dotnet\src\webdriver\DevTools\DevToolsSession.cs:line 593
   at OpenQA.Selenium.DevTools.DevToolsSession.MonitorMessageQueue() in D:\SeleniumHQ\selenium\dotnet\src\webdriver\DevTools\DevToolsSession.cs:line 541

(modified postData property).

The issue is not reproducible in Selenium v4.23 where Newtonsoft.Json is used for deserialization, and begins to be reproducible in v4.24 where System.Text.Json is used.