在硒中获取页面对象元素的Null值

时间:2019-10-02 18:21:38

标签: java selenium pageobjects

我正在一个基于黄瓜,TestNG,硒的项目中工作,我的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()时,出现零点错误。不知道为什么有什么想法要解决这个问题?

3 个答案:

答案 0 :(得分:1)

在Shoppingpage类中,使用WebElement作为类型,而不是如下所述的类名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());
        }
    }

答案 1 :(得分:0)

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

1)如果尚未加载带有new ShoppingPage(driver)的页面片段,则调用"//span[@class='qa708e IYWnmd']"太早了。

2)可能是XPath定义不正确,并且页面上没有这样的元素。检查您的XPath。

答案 2 :(得分:0)

感谢@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());

        }
    }
}