我的编码找不到元素。请任何人帮助我摆脱我的错误。
测试用例
@Test
public void main() throws Exception {
Thread.sleep(5000);
System.out.println("Before sign in action execution");
SignIn_Action.Execute(iTestCaseRow);
}
SignIn_Action
public class SignIn_Action{
public static void Execute(int iTestCaseRow) throws Exception{
// Fetch user name from Excel
String sUserName = ExcelUtils.getCellData(iTestCaseRow, Constant.col_UserName);
System.out.println("User Name read from --> "+ sUserName);
// Fetch password from Excel
String sPassword = ExcelUtils.getCellData(iTestCaseRow, Constant.col_Password);
System.out.println("Password read from excel --> "+ sPassword);
LoginPageObjects.txtbx_username().sendKeys(sUserName);
LoginPageObjects.txtbx_password().sendKeys(sPassword);
LoginPageObjects.btn_login().click();
}
LoginPageObject类
public class LoginPageObjects extends BaseClass {
private static WebElement element = null;
public LoginPageObjects(WebDriver driver){
super(driver);
}
public static WebElement txtbx_username(){
try {
WebElement element = null;
System.out.println("Inside txtbx_username method");
element = driver.findElement(By.name("first_name"));
System.out.println("Fetched element name is : "+element);
Log.info("Username text box found");
}catch (Exception e){
Log.error("UserName text box is not found on the Login Page");
throw(e);
}
return element;
}
}
}
BaseClass的
public class BaseClass {
public static WebDriver driver;
public static boolean bResult;
public BaseClass(WebDriver driver){
BaseClass.driver = driver;
BaseClass.bResult = true;
}
}
我的问题在以下代码中
element = driver.findElement(By.name("first_name"));
此代码不是find元素,它显示在下面的错误消息
java.lang.NullPointerException
at pageObjects.LoginPageObjects.txtbx_username(LoginPageObjects.java:27)