java.lang.NullPointerException 无法调用“org.openqa.selenium.WebElement”因为 this.webelement = null;
我正在尝试在google字段上发送Keys(),并且能够使用正常的xpath来执行此操作,例如:-
driver.findElement(By.name("q")).sendKeys("Gmail");
但在使用 @FindBy 方法时出现 nullpointerException :-
@FindBy(how = How.XPATH, using = "//input[@name='q']")
公共 WebElement googlesearchfield;
googlesearchfield.sendKeys("Gmail");
package BasicTestCases;
import org.testng.annotations.Test;
import pages.*;
import Utility.TestConfiguration;
import bsh.This;
public class TC02_Login extends TestConfiguration{
// private GooglePage google;
@Test
public void Login()
{
try
{
driver.get(prop.getProperty("Google"));
GooglePage googlePage = new GooglePage();
googlePage.gmaillogin();
waitforpageload();
System.out.println("end of login tc02");
}
catch (Exception e) {
// TODO: handle exception
}
}
}
package pages;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import Utility.TestConfiguration;
public class GooglePage extends TestConfiguration{
@FindBy(how = How.XPATH, using = "//input[@name='q']")
public WebElement googlesearchfield;
@FindBy(how = How.XPATH, using = "//a[text()='Gmail']")
public WebElement gmailclick;
@FindBy(how = How.XPATH, using = "//a[text()='Gmail']")
public WebElement tryf;
public void gmaillogin()
{
try {
googlesearchfield.sendKeys("Gmail");
System.out.println("entered");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我无法解决这个空指针异常为什么会来。
I am trying to sendKeys() on google field, and able to do so with normal xpath like :-
driver.findElement(By.name("q")).sendKeys("Gmail");
but getting nullpointerexception while doing with @FindBy method :-
@FindBy(how = How.XPATH, using = "//input[@name='q']")
public WebElement googlesearchfield;
googlesearchfield.sendKeys("Gmail");
package BasicTestCases;
import org.testng.annotations.Test;
import pages.*;
import Utility.TestConfiguration;
import bsh.This;
public class TC02_Login extends TestConfiguration{
// private GooglePage google;
@Test
public void Login()
{
try
{
driver.get(prop.getProperty("Google"));
GooglePage googlePage = new GooglePage();
googlePage.gmaillogin();
waitforpageload();
System.out.println("end of login tc02");
}
catch (Exception e) {
// TODO: handle exception
}
}
}
package pages;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import Utility.TestConfiguration;
public class GooglePage extends TestConfiguration{
@FindBy(how = How.XPATH, using = "//input[@name='q']")
public WebElement googlesearchfield;
@FindBy(how = How.XPATH, using = "//a[text()='Gmail']")
public WebElement gmailclick;
@FindBy(how = How.XPATH, using = "//a[text()='Gmail']")
public WebElement tryf;
public void gmaillogin()
{
try {
googlesearchfield.sendKeys("Gmail");
System.out.println("entered");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
I am not able to solve this null pointer exception why it is coming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论