在 selenium 中使用 FindBy 时出错。报错信息空点异常

Getting error when using FindBy in selenium. Error message null point exception

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;
    @FindBy(xpath= "//*[@class='hotelApp ']")
    public static WebElement hotelLink;


    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

       hotelLink.click();
        driver.quit();

    }
}

第 "HotelLink.click" 行出现错误,hotelLink 元素是使用 findBy 注释定义的,但出现 "java.lang.NullPointerException" 错误

由于您正在使用 @FindBy 注释,因此在使用之前需要初始化所有 Web 元素。

HotelBookingTest class 创建构造并使用 PageFactory 初始化,如下所示:

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;

    @FindBy(xpath= "//*[@class='hotelApp ']")
    public WebElement hotelLink;

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

    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        new HotelBookingTest(driver);
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

        hotelLink.click();
        driver.quit();
    }
}

从相应的包中导入 PageFactory 并删除 `hotelLink.

之前的 static

希望对您有所帮助...

因为你使用了@FindBy注解 使用前必须先初始化元素。

您可以通过创建接受 WebDriver 类型作为参数的参数化构造函数来做到这一点。

        PageFactory.initElements(driver, this);```

and call this constructor after opening the browser.
i.e after this line 
```driver = new ChromeDriver();```

对于 @FindBy 注释,您需要在搜索 WebElement 之前实现它。

您可以添加以简单的方式为您完成的方法:

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

import org.openqa.selenium.support.PageFactory;

public class HotelBookingTest {
    WebDriver driver;
    @FindBy(xpath= "//*[@class='hotelApp ']")
    public static WebElement hotelLink;


    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        HotelBookingTest.setPageObject(driver);
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

       hotelLink.click();
        driver.quit();

    }

    public static void setPageObject (WebDriver wd) {
        PageFactory.initElements(wd, new HotelBookingTest ());
    }
}