gpt4 book ai didi

java.lang.NullPointerException 在 org.openqa.selenium.support.pagefactory.findElement(DefaultElementLocator.java :69)

转载 作者:行者123 更新时间:2023-12-02 10:52:01 25 4
gpt4 key购买 nike

当我执行以下代码时,出现 NullPointerException。请帮我解决问题。我已经使用页面对象模型完成了。无法执行进一步的操作。

浏览器.java

public class browser {

public WebDriver driver= null;

public void initialize() {
System.setProperty("webdriver.gecko.driver","C:\\Users\\Teddy\\Downloads\\geckodriver-v0.21.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.google.com");
System.out.println("Application title is ============="+driver.getTitle());
}

public void window_close() {
driver.quit();
}
}

登录对象.java

public class loginobjects extends browser {

public loginobjects(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}

@FindBy(id="lst-ib")
public WebElement name;

@FindBy(name="btnK")
public WebElement name2;

public void login(){
name.sendKeys("Selenium with Java");
name2.click();
}
}

登录测试用例

public class logintestcase extends browser {
public loginobjects log;

@Test
public void details() {
log= new loginobjects(driver);
super.initialize();
System.out.println("here");
log.login();
System.out.println("here");
super.window_close();
}
}

异常

java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy7.click(Unknown Source)
at loginobjects.login(loginobjects.java:28)
at logintestcase.details(logintestcase.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)

最佳答案

好的,您在初始化之前已将驱动程序传递给登录对象,因此您会收到错误。

编辑logintestcase类如下,将初始化放在传递驱动程序之前。

public class logintestcase extends browser {
public loginobjects log;

@Test
public void details()

{
super.initialize();
log= new loginobjects(driver);
System.out.println("here");
log.login();
System.out.println("here");
super.window_close();


}


}

关于java.lang.NullPointerException 在 org.openqa.selenium.support.pagefactory.findElement(DefaultElementLocator.java :69),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52103151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com