I'm trying to locate a field to upload a file, but the element is inside a shadow-root
I tried the following code but I get java.lang.NullPointerException
on shadowRoot.findElement
:
private static WebElement getShadowRoot(WebDriver driver, WebElement shadowHost) {
JavascriptExecutor js = (JavascriptExecutor) driver;
return (WebElement) js.executeScript("return arguments[0].shadowRoot", shadowHost);
}
public void uploadFileWithIncorrectBirthYear() throws TestingException {
String url = System.getProperty("user.dir")+"\\src\\test\\resources\\files\\customer-upload-old-birth-year.xls";
WebElement shadowHost = driver.findElement(By.cssSelector("#cphContent_cifCustomerFile_fakeFileInput"));
SearchContext shadowRoot = getShadowRoot(driver,shadowHost);
WebElement shadowElement = shadowRoot.findElement(By.cssSelector("#file-upload-button"));
shadowElement.sendKeys(url);
}