I am trying to print nested shadow element text using below code but no luck. I have tried to use same code throough console windows and its giving me expected text.
Code :
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);
// 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();
}
}
2
Answers
Your script
document.querySelector....innerText
seems to just be accessing the value and doing nothing with it. If you want to print the value in the browser console, you need to wrap it withconsole.log()
.In selenium it returns as
webelement
you can use.getText()
or.getAttribute()
to get the value.Inline this should work as well
Or