2

Im new to selenium.

Im writing a test wherein the test needs to interact with the "print preview" window of Chrome browser that is launched when a print command is triggered. To make the tests work I have to access the DOM of the "print preview" window. The DOM contains a nested hierarchy of Shadow elements. I am able to access the Shadow elements but my code looks ugly. I understand that the shadow elements cannot be accessed directly but one needs to drill down to the child starting from the root.Is there and efficient and a cleaner way to access the DOM?

As an example -

enter image description here

I would like to access the options in the dropdown of the Destination field as shown in the image above.

For this i wrote this statement with runs perfectly -

driver.executeScript("document.querySelector('print-preview-app')
.shadowRoot.querySelector('print-preview-sidebar')
.shadowRoot.querySelector('print-preview-destination-settings')
.shadowRoot.querySelector('print-preview-settings-section').querySelector('print-preview-destination-select')
.shadowRoot.querySelector('select').options[1].text");

Below is the screenshot of the the "print preview" window code -

enter image description here

This works but looks dirty. Any better ways of digging out the required element?

Archit Arora
  • 2,508
  • 7
  • 43
  • 69

0 Answers0