0

I have a page that contains few document fragments. I want to get all elemnts from the page taht matches the selector, like this.

document.querySelectorAll('iframe')

But it no returns elements from inner document fragments. I found that I can use this:

hostElement.shadowRoot.querySelectorAll('iframe')

But I need all elements and have no idea what elements on page are hosts. How can I get all matched elements across document fragments from "top" doucment? I am using selenium webdriver, so if there is some solution related with it, it also would be enough.

Lucas
  • 81
  • 7
  • If the shadowDOM is created with ``mode:'closed'`` you can not get its contents (from outside) https://stackoverflow.com/questions/39931284/what-is-the-difference-between-open-and-closed-shadow-dom-encapsulation-mode – Danny '365CSI' Engelman Oct 14 '19 at 13:07
  • Yeap, but if I can swicth to shadow root and then use query it means it is not closed. Only solution I figure out is iteration thorugh all nodes and if element is host, switch to document fragment and do it recursively, but I don't want to solve it that way, it is to heavy. – Lucas Oct 14 '19 at 13:44

1 Answers1

0

Only way I found was query all elements and for every that have shadow root, switched to it and then query desired elements. Works and is not as slow as I expected, but I don't like this solution, it is not efficient. If anyone have better solution it would be nice.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lucas
  • 81
  • 7
  • **only if all Elements are yours** You could add an eventListener (on document) in all that return matching DOM elements... BUT.. you then need more logic to figure out when all Elements have 'answered' – Danny '365CSI' Engelman Oct 15 '19 at 09:43