Despite my efforts, I have been unable to successfully print the text from a nested shadow element using the code below. When attempting to execute the same code through the console window, it works as expected.
https://i.sstatic.net/Jlek1.png
Here is the code snippet:
import org.openqa.selenium.JavascriptException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ShadowRoot {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--remote-allow-origins=*", "ignore-certificate-errors");
driver = new ChromeDriver(chromeOptions);
// Set browser type and chromedrover.exe path as parameters
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
String url = "http://watir.com/examples/shadow_dom.html";
driver.get(url);
JavascriptExecutor js = (JavascriptExecutor) driver;
// js.executeScript("document.querySelector('#shadow_host').shadowRoot.querySelector('input').value='username'");
js.executeScript("document.querySelector('#shadow_host').shadowRoot.querySelector('#nested_shadow_host').shadowRoot.querySelector('#nested_shadow_content > div').innerText");
Thread.sleep(10000);
driver.close();
}
}