java.lang.NullPointerException 无法调用“org.openqa.selenium.WebElement”因为 this.webelement = null;

发布于 2025-01-17 11:38:35 字数 1978 浏览 2 评论 0原文

我正在尝试在google字段上发送Keys(),并且能够使用正常的xpath来执行此操作,例如:-

driver.findElement(By.name("q")).sendKeys("Gmail");

但在使用 @FindBy 方法时出现 nullpointerException :-

@FindBy(how = How.XPATH, using = "//input[@name='q']") 公共 WebElement googlesearchfield; googlesearchfield.sendKeys("Gmail");

package BasicTestCases;

import org.testng.annotations.Test;
import pages.*;
import Utility.TestConfiguration;
import bsh.This;

public class TC02_Login extends TestConfiguration{

//  private GooglePage google;
    
    
    @Test
    public void Login()
    {
        try
    {
        driver.get(prop.getProperty("Google"));
        GooglePage googlePage = new GooglePage();
        googlePage.gmaillogin();
        waitforpageload();
        System.out.println("end of login tc02");
        
    }
        catch (Exception e) {
            // TODO: handle exception
        }
    }
    
}
package pages;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

import Utility.TestConfiguration;

public class GooglePage extends TestConfiguration{
    
    
    @FindBy(how = How.XPATH, using = "//input[@name='q']")
    public WebElement googlesearchfield;
    

    @FindBy(how = How.XPATH, using = "//a[text()='Gmail']")
    public WebElement gmailclick;
    
    @FindBy(how =  How.XPATH, using = "//a[text()='Gmail']")
    public WebElement tryf;
    
    public void gmaillogin()
    {
        try {
            googlesearchfield.sendKeys("Gmail");

        System.out.println("entered");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
}

在此处输入图像描述

我无法解决这个空指针异常为什么会来。

I am trying to sendKeys() on google field, and able to do so with normal xpath like :-

driver.findElement(By.name("q")).sendKeys("Gmail");

but getting nullpointerexception while doing with @FindBy method :-

@FindBy(how = How.XPATH, using = "//input[@name='q']")
public WebElement googlesearchfield;
googlesearchfield.sendKeys("Gmail");

package BasicTestCases;

import org.testng.annotations.Test;
import pages.*;
import Utility.TestConfiguration;
import bsh.This;

public class TC02_Login extends TestConfiguration{

//  private GooglePage google;
    
    
    @Test
    public void Login()
    {
        try
    {
        driver.get(prop.getProperty("Google"));
        GooglePage googlePage = new GooglePage();
        googlePage.gmaillogin();
        waitforpageload();
        System.out.println("end of login tc02");
        
    }
        catch (Exception e) {
            // TODO: handle exception
        }
    }
    
}
package pages;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

import Utility.TestConfiguration;

public class GooglePage extends TestConfiguration{
    
    
    @FindBy(how = How.XPATH, using = "//input[@name='q']")
    public WebElement googlesearchfield;
    

    @FindBy(how = How.XPATH, using = "//a[text()='Gmail']")
    public WebElement gmailclick;
    
    @FindBy(how =  How.XPATH, using = "//a[text()='Gmail']")
    public WebElement tryf;
    
    public void gmaillogin()
    {
        try {
            googlesearchfield.sendKeys("Gmail");

        System.out.println("entered");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
}

enter image description here

I am not able to solve this null pointer exception why it is coming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文