Lingaraj R M
Lingaraj R M

Reputation: 1115

@FindBy finding webelement returns null

Hi I'm using @FindBy annotation in my selenium code

@FindBy(how=How.ID, using = "username")
public WebElement username;

System.out.println(username); - it is printing null searchBox.sendKeys("test"); - throwing "java.lang.NullPointerException"

And username webelement need to be send to another class to check visibility of it, before performing some action on it.

public static WebElement waitAndFindElement(WebDriver driver, WebElement element, int timeOutInSeconds){
WebDriverWait wait = new WebDriverWait(driver,timeOutInSeconds);
WebElement e = wait.until(ExpectedConditions.visibilityOf(element));
return e;
}

Nothing is working for me..

Upvotes: 1

Views: 3301

Answers (2)

subhasmita
subhasmita

Reputation: 1

You should pass the locator argument inside the visibilityOf method()

WebElement e = wait.until(ExpectedConditions.visibilityOf(By.id(locatorArg)));

If you dont pass the value then how can webdriver locate the element.

Upvotes: 0

Yi Zeng
Yi Zeng

Reputation: 32885

Looks like you didn't call PageFactory.initElements();

Please refer to PageFactory documentation for more details.

As you haven't post your page class which contains the elements, so I can't give you the best answer on where to put it (in general, it will be inside page object's constructor or after page object's initialization).

Upvotes: 4

Related Questions