0
WebElement myElement = driver.findElement(By.cssSelector(usernameSelectorPath));

usernameSelectorPath is a String variable which I have created in the fields, and I am having the user of the program enter the selector of the username box and store that as usernameSelectorPath. However, when I take that usernameSelectorPath to convert it to a WebElement, I get a java.lang.NullPointerException and I am unable to do it. I think it is because the variable type is a string, and it is a reference variable.

How can I fix this problem and convert String to a WebElement?

Bian Lee
  • 1
  • 2
  • no, the problem is, the variable you use is not initialized. it is null. – Stultuske Jan 03 '20 at 07:09
  • @Stultuske which variable are you talking about? I have tried initializing usernameSelectorPath variable but I still get this error. – Bian Lee Jan 03 '20 at 07:12
  • I don't know. Check your stacktrace, and it'll show you the exact line where the NPE is thrown. You can deduct it from that. – Stultuske Jan 03 '20 at 07:57
  • @BianLee check "driver" and "By" objects. One of them should be null. If "By.cssSelector" method is a static method, then the problem should "driver" is getting null. – sugeesh Jan 03 '20 at 09:11

0 Answers0