当@FindBy与静态WebElement一起使用时,将显示Nullpointer异常

时间:2019-09-13 06:26:05

标签: selenium findby

NullPointer Exception is displayed when static webelement is used with @FindBy  

Using driver.findElement.By.id("usr"), 

我能够找到元素和sendkeys     但是usernameTB.sendKeys(username)抛出了空指针异常

public class LoginPage extends TestBase {

    // Page Factory or Object Repository
    @FindBy(id = "usr")
        static WebElement  usernameTB;
public LoginPage() {
        PageFactory.initElements(driver, this);
    }
public    static void loginFunction(String env, String username, String password) {


        try
        {

        if (env != null && username!=null && password!=null) {
            if (env.equals("dev")) {
                driver.findElement(By.id("usr")).sendKeys(username);                
                usernameTB.sendKeys(username);}}   catch(Exception e)
     {
      e.printstacktrace();
     }}

0 个答案:

没有答案