亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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條經驗 獲得超6個贊

對于@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 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號