[ Bug]: v9 Deep Selectors Don't Work
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
WebdriverIO Version
9.14.0
Node.js Version
22.13.1
Mode
WDIO Testrunner
Which capabilities are you using?
{
browserName: 'chrome',
},
{
browserName: 'firefox',
},What happened?
I created a repository where I reproduce wdio bugs: https://github.com/htho/wdio-repro-collection
I want to run assertions on elements in a shadow root. According to the documentation it should be very simple:
warning
Starting with v9 of WebdriverIO there is no need for this special selector as WebdriverIO automatically pierces through the Shadow DOM for you. It is recommended to migrate off this selector by removing the >>> in front it.
For me a selector would look like this: #headlineInShadow. If I want to be more specific I could write something like this: #wrapper #headlineInShadow.
The elements are not found.
I played with classic piercing selectors and >>>#headlineInShadow works. While #wrapper>>>#headlineInShadow does not (which would be useful IMO, but I don't know if this was ever intended).
What is your expected behavior?
According to the documentation #headlineInShadow should work. But I'd also lilke #wrapper #headlineInShadow to work, because shadow-doms might repeat.
How to reproduce the bug.
I created a repository that I reuse to report bugs: https://github.com/htho/wdio-repro-collection
- git clone https://github.com/htho/wdio-repro-collection.git
- cd wdio-repro-collection
- npm ci
- npm run test:iframeInShadow
<!-- https://htho.github.io/wdio-repro-collection/iframeInShadow.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IFrame in Shadow DOM</title>
</head>
<body>
<h1>IFrame in Shadow DOM</h1>
<div id="wrapper">
<template shadowrootmode="open">
<h2 id="headlineInShadow">Headline in Shadow Root</h2>
<iframe src="iframeTarget.html"></iframe>
</template>
</div>
</body>
</html><!-- https://htho.github.io/wdio-repro-collection/iframeTarget.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Iframe Target</title>
</head>
<body>
<h1>Iframe Target</h1>
</body>
</html>beforeEach(async () => {
await browser.url(`https://htho.github.io/wdio-repro-collection/iframeInShadow.html`)
});
describe('select an element in the shadow dom', () => {
it(`works using direct piercing as introduced in v9 with "child" (fails in 9.14.0)`, async () => {
const $el = $("#headlineInShadow");
await expect($el).toExist(); // FAILS
await expect($el).toHaveText("Headline in Shadow Root");
});
it(`works using direct piercing as introduced in v9 with "parent child" (fails in 9.14.0)`, async () => {
const $el = $("#wrapper #headlineInShadow");
await expect($el).toExist(); // FAILS
await expect($el).toHaveText("Headline in Shadow Root");
});
it(`works using classic piercing with ">>>child" (works in 9.14.0)`, async () => {
const $el = $(">>>#headlineInShadow");
await expect($el).toExist();
await expect($el).toHaveText("Headline in Shadow Root");
});
it(`works using classic piercing with "parent>>>child" (fails in 9.14.0)`, async () => {
const $el = $("#wrapper>>>#headlineInShadow");
// FAILS in chrome: WebDriverError: invalid selector: An invalid or illegal selector was specified (Session info: chrome=135.0.7049.115) when running "element" with method "POST" and args "{"using":"css selector","value":"#wrapper>>>#headlineInShadow"}"
// FAILS in firefox: WebDriverError: Given css selector expression "#wrapper>>>#headlineInShadow" is invalid: InvalidSelectorError: Document.querySelector: '#wrapper>>>#headlineInShadow' is not a valid selector: "#wrapper>>>#headlineInShadow" when running "element" with method "POST" and args "{"using":"css selector","value":"#wrapper>>>#headlineInShadow"}"
await expect($el).toExist();
await expect($el).toHaveText("Headline in Shadow Root");
});
it(`works using $("parent").shadow$("child") (works in 9.14.0)`, async () => {
const $el = $("#wrapper").shadow$("#headlineInShadow");
await expect($el).toExist();
await expect($el).toHaveText("Headline in Shadow Root");
});
});Relevant log output
included as comment in the test file
Code of Conduct
- I agree to follow this project's Code of Conduct
Is there an existing issue for this?
- I have searched the existing issues
Source: webdriverio/webdriverio