org.openqa.selenium.support.pagefactory.findElement的java.lang.NullPointerException(DefaultElementLoc

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了org.openqa.selenium.support.pagefactory.findElement的java.lang.NullPointerException(DefaultElementLoc相关的知识,希望对你有一定的参考价值。

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

browser.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();
    }
}

login objects.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();
    }
}

logintestcase

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)
答案

好的,所以你在初始化之前已经将驱动程序传递给loginobjects,这样你就得到了错误。

通过在驱动程序传递之前进行初始化来编辑登录测试用例类,如下所示。

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


        }


    }

以上是关于org.openqa.selenium.support.pagefactory.findElement的java.lang.NullPointerException(DefaultElementLoc的主要内容,如果未能解决你的问题,请参考以下文章