无法通过在网页上找到元素来调用

发布于 2025-02-04 18:17:17 字数 4595 浏览 2 评论 0原文

我使用PageObject从头开始编写硒项目。

以下代码失败了,因为Selenium WebDriver在网页上找不到元素。

这是我遇到的错误:

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null

driverbase.java:

在此类I设置驱动程序

package com.EbankingA11y.base;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.EbankingA11y.util.WebListener;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class DriverBase {
       
        protected static final Logger LOG = (Logger) LogManager.getLogger(WebListener.class);

        public static WebDriver driver;
        public static Properties prop;
        public static FileInputStream fis;
        
        public DriverBase()  {
            
            prop = new Properties();
            
            try {
                fis = new FileInputStream("C:\\Users\\config.properties");
            } catch (FileNotFoundException exception) {
                exception.printStackTrace();
                LOG.debug(" Error \n " +exception.getStackTrace());

            }
            try {
                prop.load(fis);
            } catch (IOException exception) {
                exception.printStackTrace();
                LOG.debug(" Error \n " +exception.getStackTrace());
            }
        }
        
        public static void initialization () throws InterruptedException {
            System.setProperty("webdriver.chrome.driver", prop.getProperty("WEB_DRIVER_PATH") );
            //WebDriverManager.chromedriver().setup();
            
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.setAcceptInsecureCerts(true);

            driver = new ChromeDriver(chromeOptions);
            driver.manage().window().maximize();
            driver.get(prop.getProperty("URL"));
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
    }

loginpage.java.java

在此类中我在登录页面上创建所有元素和所有方法

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.EbankingA11y.base.DriverBase;

public class LoginPage extends DriverBase {

    public LoginPage() {
        PageFactory.initElements(DriverBase.driver, this);
    }
    @FindBy(name="username")
    WebElement usernameTextBox;
    
    @FindBy(name="password")
    WebElement passwordTextBox;
    
    @FindBy(xpath="//input[@value=\\\"Use 'Enter' to confirm\\\"]")
    WebElement submitButton;
    
    @FindBy(xpath="//button[@name='button']")
    WebElement languageButton;  
            
    @FindBy(name="smsCode")
    WebElement tokenTextBox;


    public void performLogin () {
        //WebDriverWait wait = new WebDriverWait(driver,30);

        final String username = prop.getProperty("USERNAME");
        final String password = prop.getProperty("PASSWORD");
        System.out.println(username);

        //wait.until(ExpectedConditions.visibilityOf(this.usernameTextBox));
        usernameTextBox.clear();
        passwordTextBox.clear();
        usernameTextBox.sendKeys(username);
        passwordTextBox.sendKeys(password);
        
        submitButton.click();
        
        //wait.until(ExpectedConditions.visibilityOf(this.tokenTextBox));

    }
}

loginpagetests.java:

在此类中我编写测试,我从loginpage.java类调用方法

package com.EbankingA11y.testcases;

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.EbankingA11y.base.*;
import com.EbankingA11y.pages.LoginPage;


public class LoginPageTest extends DriverBase {
    

    public LoginPageTest() throws IOException {
        super();
    }
    
    LoginPage loginPage = new LoginPage();
    
    @BeforeTest
    public void setup() throws InterruptedException {
        initialization();
    }

    @Test
    public void login()  {
        loginPage.performLogin();
    }
}

Im writing a selenium project from scratch using the PageObject.

The below code is failing because the selenium webdriver is not finding elements on the webpage .

This is the error i get :

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null

DriverBase.java :

In this class i setup the driver

package com.EbankingA11y.base;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.EbankingA11y.util.WebListener;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class DriverBase {
       
        protected static final Logger LOG = (Logger) LogManager.getLogger(WebListener.class);

        public static WebDriver driver;
        public static Properties prop;
        public static FileInputStream fis;
        
        public DriverBase()  {
            
            prop = new Properties();
            
            try {
                fis = new FileInputStream("C:\\Users\\config.properties");
            } catch (FileNotFoundException exception) {
                exception.printStackTrace();
                LOG.debug(" Error \n " +exception.getStackTrace());

            }
            try {
                prop.load(fis);
            } catch (IOException exception) {
                exception.printStackTrace();
                LOG.debug(" Error \n " +exception.getStackTrace());
            }
        }
        
        public static void initialization () throws InterruptedException {
            System.setProperty("webdriver.chrome.driver", prop.getProperty("WEB_DRIVER_PATH") );
            //WebDriverManager.chromedriver().setup();
            
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.setAcceptInsecureCerts(true);

            driver = new ChromeDriver(chromeOptions);
            driver.manage().window().maximize();
            driver.get(prop.getProperty("URL"));
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
    }

LoginPage.java

In this class i create all the element on the login page and all methods

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.EbankingA11y.base.DriverBase;

public class LoginPage extends DriverBase {

    public LoginPage() {
        PageFactory.initElements(DriverBase.driver, this);
    }
    @FindBy(name="username")
    WebElement usernameTextBox;
    
    @FindBy(name="password")
    WebElement passwordTextBox;
    
    @FindBy(xpath="//input[@value=\\\"Use 'Enter' to confirm\\\"]")
    WebElement submitButton;
    
    @FindBy(xpath="//button[@name='button']")
    WebElement languageButton;  
            
    @FindBy(name="smsCode")
    WebElement tokenTextBox;


    public void performLogin () {
        //WebDriverWait wait = new WebDriverWait(driver,30);

        final String username = prop.getProperty("USERNAME");
        final String password = prop.getProperty("PASSWORD");
        System.out.println(username);

        //wait.until(ExpectedConditions.visibilityOf(this.usernameTextBox));
        usernameTextBox.clear();
        passwordTextBox.clear();
        usernameTextBox.sendKeys(username);
        passwordTextBox.sendKeys(password);
        
        submitButton.click();
        
        //wait.until(ExpectedConditions.visibilityOf(this.tokenTextBox));

    }
}

LoginPageTests.java :

In this class i write tests and i call methods from LoginPage.java Class

package com.EbankingA11y.testcases;

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.EbankingA11y.base.*;
import com.EbankingA11y.pages.LoginPage;


public class LoginPageTest extends DriverBase {
    

    public LoginPageTest() throws IOException {
        super();
    }
    
    LoginPage loginPage = new LoginPage();
    
    @BeforeTest
    public void setup() throws InterruptedException {
        initialization();
    }

    @Test
    public void login()  {
        loginPage.performLogin();
    }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2025-02-11 18:17:17

之后以前方法声明页面

public class LoginPageTest extends DriverBase {
    

    public LoginPageTest() throws IOException {
        super();
    }
    

    // here
    LoginPage loginPage; 
    TokenPage tokenPage;
    
    @BeforeTest
    public void setup() throws InterruptedException {
        initialization();
      

         //here
         loginPage = new LoginPage(); 
         tokenPage = new TokenPage();
    }

    @Test
    public void login()  {
    tokenPage = loginPage.performLogin();
        
}
}

我发现我必须在Initilisation()

I found that i have to declare the pages in the before methods after the initilisation ()

public class LoginPageTest extends DriverBase {
    

    public LoginPageTest() throws IOException {
        super();
    }
    

    // here
    LoginPage loginPage; 
    TokenPage tokenPage;
    
    @BeforeTest
    public void setup() throws InterruptedException {
        initialization();
      

         //here
         loginPage = new LoginPage(); 
         tokenPage = new TokenPage();
    }

    @Test
    public void login()  {
    tokenPage = loginPage.performLogin();
        
}
}

This is just worked for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文