3

This is the Java code I have:

package test;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class FirstSeleniumTest {

public static WebDriver driver;

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver","C:\\Users\\35196\\IdeaProjects\\selenium test\\SeleniumTest\\lib\\seleniumjars\\chromedriver.exe");

    driver = new ChromeDriver();

    driver.navigate().to("https://foodplanhealth.herokuapp.com/login");

    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    WebElement root1 = driver.findElement(By.cssSelector("my-app"));
    WebElement shadow_root1 = expand_shadow_element(root1);

    WebElement root2 = shadow_root1.findElement(By.cssSelector("mwc-drawer"));
    WebElement shadow_root2= expand_shadow_element(root2);

    WebElement root3 = shadow_root2.findElement(By.name("appContent"));
    WebElement shadow_root3 = expand_shadow_element(root3);

    WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content"));
    WebElement shadow_root4 = expand_shadow_element(root4);

    WebElement root5 = shadow_root4.findElement(By.cssSelector("div#container"));
    WebElement shadow_root5 = expand_shadow_element(root5);

    WebElement root6 = shadow_root5.findElement(By.cssSelector("login-container"));
    WebElement shadow_root6 = expand_shadow_element(root6);

    WebElement root7 = shadow_root6.findElement(By.cssSelector("mwc-textfield"));
    WebElement shadow_root7 = expand_shadow_element(root7);

    WebElement root8 = shadow_root7.findElement(By.cssSelector("mdc-text-field__ripple"));
    WebElement shadow_root8 = expand_shadow_element(root8);

    WebElement login = shadow_root8.findElement(By.cssSelector("mdc-text-field__input"));

    String js = "arguments[0].setAttribute('value','[email protected]')";
    ((JavascriptExecutor) driver).executeScript(js, login);

}

public static WebElement expand_shadow_element(WebElement element)
{
    return (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", element);
}
}

The code runs fine up until line 35 WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content")); where i get this error

Exception in thread "main" java.lang.NullPointerException at test.FirstSeleniumTest.main(FirstSeleniumTest.java:35)

I know the code in its entirety is not correct and incomplete, but I can't move forward because of this error. I just don't understand why it works fine up until this point but it doesn't in this. I asked someone for help and they told me the code runs fine for them, I checked the chrome driver version and it matches the browser version.

Can someone tell me what is wrong?

2
  • div#main-content is it tagName? looks like selector Commented Apr 9, 2021 at 15:38
  • Something on line 35 is null, and you can't call methods on a null object Commented Apr 9, 2021 at 20:46

2 Answers 2

2

A NullPointerException always implies that the null pointer has been dereferenced, either by evaluating a period after a null reference, either by indexing a null as an array.

Let's study this case in your source line:

WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content")); 

There are just two occurrences of the derefence operator (the period): We must discard the one after By, because it is a class and cannot be null. So we are left with just one culprit: shadow_root3 is surely null.

Next step: Why shadow_root3 is null? It is initialized in this previous line:

root3 = shadow_root2.findElement(By.name("appContent"));
WebElement shadow_root3 = expand_shadow_element(root3);

Method expand_shadow_element returns the shadowRoot of the inputted argument (or null if it has none), so we must conclude that the DOM element named appContent has no shadowRoot set.

From here on, you must debug your HTML page to diagnose why it has no shadowRoot, and how to set it properly.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, the element I was trying to shadow root wasn't in the shadow tree. The other comment helped me correct the issue. Thanks a lot though!
1

Its not able to find element due to which it throws the null pointer exception. You may need to change the xpath or by the locator, you are identifying the element.

Below is the code to access the email text box

String cssSelectorForHost1 = "my-app[title='FoodPlan']";
String cssSelectorForHost2 = "page-login";
String cssSelectorForHost3 = "mwc-textfield[type='email']";
Thread.sleep(1000);
WebElement shadowDomHostElement0 = driver.findElement(By.cssSelector("my-app[title='FoodPlan']"));
WebElement last0 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement0);
Thread.sleep(1000);
WebElement shadowDomHostElement1 = last0.findElement(By.cssSelector("page-login"));
WebElement last1 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement1);
Thread.sleep(1000);
WebElement shadowDomHostElement2 = last1.findElement(By.cssSelector("mwc-textfield[type='email']"));
WebElement last2 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement2);
Thread.sleep(1000);
last2.findElement(By.cssSelector(".mdc-text-field__input"))

2 Comments

Sorry for the late feedback, but thank you very much! It worked out perfectly
if it helped you kindly select it as correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.