Selenium 在pageFactory中查找(webdriver)不';我不能正常工作

Selenium 在pageFactory中查找(webdriver)不';我不能正常工作,selenium,selenium-webdriver,junit,webdriver,selenium-chromedriver,Selenium,Selenium Webdriver,Junit,Webdriver,Selenium Chromedriver,我想用seleniumWebDriver和PageFactory编写测试,但是如果我在类中的PageFactory表单中添加注释 @FindBy(id="email") public WebElement mailLink; 使用方法: mailLink.sendKeys("mail@mail.com"); 每次我都会得到一个NullPointerException。另一种方式: driver.findElement(By.id("email")).sendKeys("mail@mail.c

我想用selenium
WebDriver
PageFactory
编写测试,但是如果我在类中的
PageFactory
表单中添加注释

@FindBy(id="email")
public WebElement mailLink;
使用方法:

mailLink.sendKeys("mail@mail.com");
每次我都会得到一个
NullPointerException
。另一种方式:

driver.findElement(By.id("email")).sendKeys("mail@mail.com");
返回正确的值。第一种方法的问题在哪里

我的代码:

我在FaceClass中进行了驱动程序初始化:

public class FaceClass {

protected WebDriver driver;

public FaceClass(WebDriver driver){
    this.driver = driver;
}

public HomePage navigateToApp(){
    driver.navigate().to("https://facebook.pl");

    return PageFactory.initElements(driver, HomePage.class);
}
}

并使用业务逻辑进行分类:

public class HomePage extends FaceClass{

public HomePage(WebDriver driver) {
    super(driver);
    // TODO Auto-generated constructor stub
}


@FindBy(id="email")
public WebElement mailLink;

@FindBy(id="pass")
public WebElement passLink;

@FindBy(how = How.ID, using="u_0_n")
public WebElement loginButton;


public ProfilePage navigateToProfile(){

    try{

        if(driver.findElement(By.id("pass")).isEnabled() || driver.findElement(By.id("pass")).isDisplayed()){
            System.out.println("ok!");
        }



    //driver.findElement(By.id("pass")).sendKeys("pass_to_account");
    //driver.findElement(By.id("email")).sendKeys("mail@mail.com");
    //driver.findElement(By.id("u_0_n")).click();
    mailLink.sendKeys("mail@mail.com");
    passLink.sendKeys("pass_to_account");
    loginButton.click();

    } catch (Exception e) {
        e.printStackTrace();
        }

    return PageFactory.initElements(driver, ProfilePage.class);
}
}

和测试:

    public class ExampleTest {

WebDriver driver;

@Before
public void setUp() throws Exception {

    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    DesiredCapabilities capabilities=DesiredCapabilities.chrome();
    capabilities.setCapability("marionette", true);
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.navigate().to("https://facebook.pl");
}

@After
public void tearDown() throws Exception {

    driver.quit();
}

@Test
public void test() {
    //fail("Not yet implemented");
    HomePage homepage = new HomePage(driver);
    homepage.navigateToProfile();

}
}


所有元素都已启用且可见

在使用之前,您没有初始化元素。要初始化页面元素,请使用PageFactory方法initElements。最好在构造函数中调用它,如下所示:

public HomePage(WebDriver driver) {
    super(driver);
    PageFactory.initElements(driver, this);
}

希望它能工作。

在前面的案例中,您是否正确初始化了驱动程序?您可以提供您的代码部分。