在 Selenium 中获取页面对象元素的空值

Getting Null value for the Page Object Element in Selenium

我在基于 Cucumber、TestNG、Selenium 的项目中工作,我的 PageObject 页面如下所示

package com.testing.pageobject;

import java.util.List;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.testing.components.SearchResultComponent;
import com.testing.stepdefinitions.BaseClass;

public class ShoppingPage extends BaseClass {



    public ShoppingPage(RemoteWebDriver driver) {
        super();
        this.driver=driver;
        PageFactory.initElements(driver, this);

    }

    @FindBy(xpath="//div[@class='sh-dlr__list-result']")
    private List<SearchResultComponent> SearchResult;

    @FindBy(xpath="//span[@class='qa708e IYWnmd']")
    private List<WebElement> ResultListView;

    @FindBy(xpath="//span[@class='Ytbez']")
    private List<WebElement> ResultGridView;

    public List<SearchResultComponent> getSearchResult() {
        return SearchResult;
    }

    public List<WebElement> getResultListView() {
        return ResultListView;
    }

    public List<WebElement> getResultGridView() {
        return ResultGridView;
    }   

}

我的组件页面如下

package com.testing.components;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.testing.stepdefinitions.BaseClass;

public class SearchResultComponent extends BaseClass {



    public SearchResultComponent(RemoteWebDriver driver) {
         super();
         this.driver=driver;
         PageFactory.initElements(driver, this);
    }

    @FindBy(xpath="//a[@class='AGVhpb']")
    private WebElement productName;

    @FindBy(xpath="//span[@class='O8U6h']")
    private WebElement productPrice;

    @FindBy(xpath="//div[@class='vq3ore']")
    private WebElement productStars;

    @FindBy(xpath="//img[@class='TL92Hc']")
    private WebElement productImage;

    @FindBy(xpath="//div[@class='hBUZL CPJBKe']")
    private WebElement productDescription;

    public WebElement getProductName() {
        return productName;
    }

    public WebElement getProductPrice() {
        return productPrice;
    }

    public WebElement getProductStars() {
        return productStars;
    }

    public WebElement getProductImage() {
        return productImage;
    }

    public WebElement getProductDescription() {
        return productDescription;
    }




}

我的黄瓜步骤定义是

@When("search for product less than {int}")
public void search_for_product_less_than(Integer int1) {
    ShoppingPage myshopping = new ShoppingPage(driver);
    List<SearchResultComponent> SearchResults = myshopping.getSearchResult();
    for(SearchResultComponent myResult:SearchResults) {
        System.out.println(myResult.getProductName());
    }
}

问题陈述:

当我尝试获取步骤定义中的 getSearchResult() 时出现空点错误。不知道为什么有任何想法如何解决这个问题?

原因是页面中没有找到需要的元素。这可能有不同的原因。

1) 你调用 new ShoppingPage(driver) 太早了,因为 "//span[@class='qa708e IYWnmd']" 的页面片段还没有加载。

2) 可能是XPath定义错误,页面上没有这个元素。检查你的 XPath。

在 Shoppingpage class 中使用 WebElement 作为类型而不是下面提到的 class 名称 SearchResultComponent

@FindBy(xpath="//div[@class='sh-dlr__list-result']")
private List<WebElement> SearchResult;

public List<WebElement> getSearchResult() {
    return SearchResult;
}

也在黄瓜步骤定义中使用 webelement

   @When("search for product less than {int}")
   public static void search_for_product_less_than(Integer int1) {
        ShoppingPage myshopping = new ShoppingPage(driver);
        List<WebElement> SearchResults = myshopping.getSearchResult();
        for(WebElement myResult:SearchResults) {
            System.out.println(myResult.getText());
        }
    }

谢谢@Yosuva A。我将它更新为 WebElement,它现在的工作方式如下

public void search_for_product_less_than(Integer int1) {
    ShoppingPage myshopping = new ShoppingPage(driver);
    SearchResultComponent resultcomp = new SearchResultComponent(driver);

    List<WebElement> PriceResult = resultcomp.getProductPrice();
    List<WebElement> ProductName = resultcomp.getProductName();
    for(int i=0;i<PriceResult.size();i++) {
        String price = PriceResult.get(i).getText().replace("$", "");
        System.out.println("Price" + Double.valueOf(price));
        if(Double.valueOf(price)<13) {
            System.out.println(price);
            System.out.println(ProductName.get(i).getText());

        }
    }
}