为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

收到一只叮咚 2022-07-14 16:29:27
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”错误
查看完整描述

3 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

当您使用@FindBy注释时,您需要在使用之前初始化所有 Web 元素。


为类创建一个构造并使用如下所示进行HotelBookingTest初始化: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从相应的包中导入并static在`hotelLink.


我希望它有帮助...


查看完整回答
反对 回复 2022-07-14
?
梦里花落0921

TA贡献1772条经验 获得超5个赞

对于@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 ());

    }

}


查看完整回答
反对 回复 2022-07-14
?
慕慕森

TA贡献1856条经验 获得超17个赞

由于您使用的是 @FindBy 注释,因此您必须在使用之前初始化元素。


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


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


and call this constructor after opening the browser.

i.e after this line 

```driver = new ChromeDriver();```


查看完整回答
反对 回复 2022-07-14
  • 3 回答
  • 0 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信