0

I want to use "FindBy" annotation to search web element objects, but when I use it, it returns null. This is what am I using:

class TestPage {
@FindBy(xpath="//*[@id=\"mat-input-0\"]")
public static WebElement identificationNumber
}

And this is how I call it:

TestPage.identificationNumber.sendKeys("TEST")

When I execute it, it prints an error saying: Cannot invoke method sendKeys() on null object.

I've tried also different method which works fine, but I don't understand why "FindBy" annotation does not work. This is working method:

def static setIdentificationNumber() {
    WebElement identificationNumber = getWebDriver().findElement(By.xpath("//*[@id=\"mat-input-0\"]"))
    def setText = identificationNumber.sendKeys("ABC")
}
brithwulf
  • 538
  • 10
  • 35
  • How is IntelliJ here relevant? Does it work if you run it with your build tool? – cfrick Aug 06 '20 at 09:28
  • I'm working in intellIJ, why is intellij not relevant here ? – brithwulf Aug 06 '20 at 10:03
  • Is the error _only_ happening in IntelliJ or is it happening _always_? If the later, then IntelliJ has no relevance. – cfrick Aug 06 '20 at 10:07
  • Sorry, but I did not understood what are you saying. I am using both methods in intellij and I am using groovy language for that. When I use "FindBy" annotation it gives me an error and when I use second method it works. I just want to know, why "FindBy" annotation does not work for me – brithwulf Aug 06 '20 at 10:11
  • Read this: https://stackoverflow.com/questions/56042096/java-and-selenium-static-methods-in-page-objects/56042221#56042221 – Fenio Aug 06 '20 at 10:28
  • @Fenio so you say that, FindBy annotation is not good to use ? But instead, it is better to use findElement which i described in the second method ? – brithwulf Aug 06 '20 at 10:56
  • You shouldn't mix @FindBy and static elements/methods. It won't work as expected – Fenio Aug 06 '20 at 10:57
  • But if I remove static so, it does not work anyway, when I create an instance like : TestPage page = new TestPage; page.identificationNumber -> it gives null. – brithwulf Aug 06 '20 at 11:33
  • Can you update your question? Remove unused code (with static declarations), leave what you have right now and add as most details about the problem as you can – Fenio Aug 07 '20 at 08:12

0 Answers0