尝试在webDriver中使用@FindBy时获取java.lang.NullPointerException
当我试图使用@FindBy注释查找网页上的元素时,我得到了java.lang.NullPointerException 我的密码-尝试在webDriver中使用@FindBy时获取java.lang.NullPointerException,java,selenium,nullpointerexception,testng,pageobjects,Java,Selenium,Nullpointerexception,Testng,Pageobjects,当我试图使用@FindBy注释查找网页上的元素时,我得到了java.lang.NullPointerException 我的密码- public class pageObject{ WebDriver driver; @FindBy(id = "email") WebElement searchBox; @FindBy(id = "u_0_v") WebElement submit; void pageObject(String web){ FirefoxPr
public class pageObject{
WebDriver driver;
@FindBy(id = "email")
WebElement searchBox;
@FindBy(id = "u_0_v")
WebElement submit;
void pageObject(String web){
FirefoxProfile profile = new FirefoxProfile();
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setAcceptUntrustedCertificates(false);
this.driver = new FirefoxDriver(profile);
this.driver.get(web);
this.driver.manage().window().maximize();
this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
searchBox.click();
searchBox.sendKeys("er");
submit.click();
}
public static void main(String[] args){
new pageObject("https://www.facebook.com/?_rdr=p");
}
}
我得到了上述代码的一个例外-
例外情况-
Exception in thread "main" java.lang.NullPointerException
at com.Selenium_Practice.pageObject.<init>(pageObject.java:29)
at com.Selenium_Practice.pageObject.main(pageObject.java:35)
但是同样出现了空指针异常如果您使用Selenium的PageFactory并且希望类是自包含的,那么首先需要初始化元素*
pageObject(String web){
FirefoxProfile profile = new FirefoxProfile();
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setAcceptUntrustedCertificates(false);
this.driver = new FirefoxDriver(profile);
// You need to put this line in your constructor
PageFactory.initElements(this.driver, this);
// Then follows the rest of your constructor
...
}
*也就是说,您也可以在类外初始化this class元素,但我认为您希望在类内初始化。先生,我将PageFactory.initElements(driver,this);在我的构造函数中作为第一行,但我在org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)的线程“main”java.lang.NullPointerException中再次获得了NullPointerException异常,该线程位于org.openqa.selenium.support.pagefactory.internal.LocatingElementLocator.invoke(LocatingElementHandler.java:38)位于com.sun.proxy.$Proxy2.单击com.Selenium_Practice.pageObject.(pageObject.java:31)位于com.Selenium_Practice.pageObject.main(pageObject.java:37)的(未知源代码)抱歉,输入错误!当然,这行代码需要在实例化驱动程序变量之后出现!->请参阅我的更新现在正在运行。先生,如何在类之外初始化此类元素?
pageObject(String web){
FirefoxProfile profile = new FirefoxProfile();
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setAcceptUntrustedCertificates(false);
this.driver = new FirefoxDriver(profile);
// You need to put this line in your constructor
PageFactory.initElements(this.driver, this);
// Then follows the rest of your constructor
...
}