@FindBy批注定义的Webelement返回空指针 [英] Webelement defined by @FindBy annotation returns null pointer

查看:214
本文介绍了@FindBy批注定义的Webelement返回空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,当我调用方法Spage.editExButton(int ID)时,出现一条错误消息,指出WebElement首先为null.为什么它为空?我已经使用@FindBy批注定义了它.为了能够单击它,我必须使用findElement(By.id("xxx"))显式定义该元素.但是,为什么我不能使用@FindBy表示法来调用它?

For some reason, when I call method Spage.editExButton(int ID), I get an error saying that WebElement first is null. Why is it null? I have defined it using the @FindBy annotation. I have to explicitly define the element using findElement(By.id("xxx")) in order to be able to click on it. But why am I unable to call it using the @FindBy notation?

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);
}

推荐答案

您需要调用它(最好在构造函数中):

You need to call this (preferably in your constructors):

PageFactory.initElements(getDriver(), this);

更多信息: https://code.google.com/p/selenium/wiki/PageFactory

这篇关于@FindBy批注定义的Webelement返回空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆