querySelector that can pierce Shadow DOM roots without knowing the path through nested shadow roots. Useful for automated testing of Web Components. Production use is not advised, this is for test environments/tools such as Web Driver, Playwright, Puppeteer
Hi @Georgegriff,
First of all thanks for sharing your code! I've been using it for a while and it's working well :)
I've come across, however, a corner case -
trying to query a selector in a shadowDOM in this website.
Querying this selector (each element of the array is a new shadowDom document)
["dps-app", "dps-home-unauth", "dps-card > img[slot="icon-custom"][height="\33 0"]"]
(please note that the last string is automatically backslashed \ into "dps-card > img[slot=\"icon-custom\"][height=\"\\33 0\"]")
works in the "classic" way to query shadow selectors:
document.querySelector("dps-app").shadowRoot.querySelector("dps-home-unauth").shadowRoot.querySelector("dps-card > img[slot=\"icon-custom\"][height=\"\\33 0\"]")
However, trying to query it with queryDeep returns null:
(after converting into a queryDeep selector syntax - "dps-app dps-home-unauth dps-card img[slot=\"icon-custom\"][height=\"\\33 0\"]")
But, when removing the selector's last funky height attribute - it succeeds!
("dps-app dps-home-unauth dps-card img[slot=\"icon-custom\"]")
I'm not sure why it has failed to query with the height attribute, and would really appreciate your help with fixing it.
Thanks!
Hi @Georgegriff, First of all thanks for sharing your code! I've been using it for a while and it's working well :)
I've come across, however, a corner case - trying to query a selector in a shadowDOM in this website. Querying this selector (each element of the array is a new
shadowDom
document)["dps-app", "dps-home-unauth", "dps-card > img[slot="icon-custom"][height="\33 0"]"]
(please note that the last string is automatically backslashed\
into"dps-card > img[slot=\"icon-custom\"][height=\"\\33 0\"]"
)works in the "classic" way to query shadow selectors:
document.querySelector("dps-app").shadowRoot.querySelector("dps-home-unauth").shadowRoot.querySelector("dps-card > img[slot=\"icon-custom\"][height=\"\\33 0\"]")
However, trying to query it with
queryDeep
returnsnull
: (after converting into aqueryDeep
selector syntax -"dps-app dps-home-unauth dps-card img[slot=\"icon-custom\"][height=\"\\33 0\"]"
)But, when removing the selector's last funky
height
attribute - it succeeds! ("dps-app dps-home-unauth dps-card img[slot=\"icon-custom\"]"
)I'm not sure why it has failed to query with the
height
attribute, and would really appreciate your help with fixing it. Thanks!