Skip to content

[🐛 Bug]: Element.clear() fails when document requires Trusted Types #11461

@tosmolka

Description

@tosmolka

What happened?

We are trying to use Selenium to instrument a website that requires Trusted Types in browser that supports Trusted Types (Chrome/Edge) and it works in most parts.

What fails is an attempt to call .clear() on element with contenteditable="true" attribute. This is (most likely) due to the fact that implementation uses empty string assignment into .innerHTML property.

element.innerHTML = goog.userAgent.GECKO ? ' ' : '';

Suggested fix is to achieve the same by other means, without assignment to .innerHTML:

element.textContent = goog.userAgent.GECKO ? ' ' : '';

or

while (element.firstChild) {
  element.removeChild(element.firstChild);
}

Or other fix, not sure about differences in behavior between browsers (Firefox). Firefox could even continue using current implementation since it does not support Trusted Types and this bug will not repro there.

How can we reproduce the issue?

const { Builder, By } = require('selenium-webdriver');

(async () => {
    const driver = await new Builder()
      .forBrowser('chrome')
      .build();
    try {
        await driver.get('data:text/html,' + `<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';" />
  </head>
  <body>
    <div id="test" contenteditable="true">Test</div>
  </body>
</html>
`);
        const element = await driver.findElement(By.id('test'));
        await element.clear();
    } finally {
        await driver.quit();
    }
})();

Relevant log output

DevTools listening on ws://127.0.0.1:60538/devtools/browser/77b3e399-feb0-42c1-840d-6f54e8a54e4e
c:\Temp\test-selenium\node_modules\selenium-webdriver\lib\error.js:524
    let err = new ctor(data.message)
              ^

JavascriptError: javascript error: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment.
  (Session info: chrome=108.0.5359.125)
    at Object.throwDecodedError (c:\Temp\test-selenium\node_modules\selenium-webdriver\lib\error.js:524:15)
    at parseHttpResponse (c:\Temp\test-selenium\node_modules\selenium-webdriver\lib\http.js:587:13)
    at Executor.execute (c:\Temp\test-selenium\node_modules\selenium-webdriver\lib\http.js:515:28)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Driver.execute (c:\Temp\test-selenium\node_modules\selenium-webdriver\lib\webdriver.js:741:17)
    at async c:\Temp\test-selenium\src\test.js:19:9 {
  remoteStacktrace: 'Backtrace:\n' +
    '\t(No symbol) [0x0093F243]\n' +
    '\t(No symbol) [0x008C7FD1]\n' +
    '\t(No symbol) [0x007BD04D]\n' +
    '\t(No symbol) [0x007BFD34]\n' +
    '\t(No symbol) [0x007BFBE5]\n' +
    '\t(No symbol) [0x007BFE80]\n' +
    '\t(No symbol) [0x007E4C87]\n' +
    '\t(No symbol) [0x0080858C]\n' +
    '\t(No symbol) [0x007E2BFF]\n' +
    '\t(No symbol) [0x00808804]\n' +
    '\t(No symbol) [0x0081C9EB]\n' +
    '\t(No symbol) [0x00808386]\n' +
    '\t(No symbol) [0x007E163C]\n' +
    '\t(No symbol) [0x007E269D]\n' +
    '\tGetHandleVerifier [0x00BD9A22+2655074]\n' +
    '\tGetHandleVerifier [0x00BCCA24+2601828]\n' +
    '\tGetHandleVerifier [0x009E8C0A+619850]\n' +
    '\tGetHandleVerifier [0x009E7830+614768]\n' +
    '\t(No symbol) [0x008D05FC]\n' +
    '\t(No symbol) [0x008D5968]\n' +
    '\t(No symbol) [0x008D5A55]\n' +
    '\t(No symbol) [0x008E051B]\n' +
    '\tBaseThreadInitThunk [0x765F7D69+25]\n' +
    '\tRtlInitializeExceptionChain [0x7768BB9B+107]\n' +
    '\tRtlClearBits [0x7768BB1F+191]\n'
}

Operating System

Windows 11

Selenium version

JavaScript 4.7.1

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

Chrome 108.0.5359.125 (Official Build) (64-bit)

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

ChromeDriver 108.0.5359.71

Are you using Selenium Grid?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-atomsJavaScript chunks generated by Google closureI-defectSomething is not working as intendedJ-awaiting answerQuestion asked of user; a reply moves it to triage againJ-staleApplied to issues that become stale, and eventually closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions