【发布时间】:2018-12-25 18:51:28
【问题描述】:
由于某种原因,当我调用方法 Spage.editExButton(int ID) 时,我收到一条错误消息,指出 WebElement first 为 null。为什么它是空的?我已经使用 @FindBy 注释定义了它。我必须使用 findElement(By.id("xxx")) 显式定义元素才能单击它。但是为什么我不能使用 @FindBy 表示法来调用它?
public class SPage extends GPage<SPage> {
public SPage() {
super();
}
public SPage(String pageType) {
super(pageType);
}
@FindBy(id = "xxx")
WebElement first;
public WebElement eButton(int ID) {
first.click();
String tmp = ID + "-Edit";
WebElement edit = getDriver().findElement(By.id(tmp));
return edit;
}
public EPage cEdit(int ID) {
eButton(ID).click();
return new EPage(getBasePageType()).openPage(EPage.class);
}
}
我这样调用方法:
static EPage epage;
static SPage spage;
@Test
public void edit_exception() {
epage = spage.cEdit(IDbefore);
}
【问题讨论】:
-
从给出的代码看来,你还没有初始化
WebElement first,所以默认情况下它的null将NPE抛出first.click(); -
你是如何创建
SPage的这个实例的?它是 Selenium 给你的吗? -
我添加了更多关于如何调用我的方法的信息。
标签: java selenium nullpointerexception webdriver selenium-webdriver