简体   繁体   中英

@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..

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM