我正在一个基于黄瓜,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()时,出现零点错误。不知道为什么有什么想法要解决这个问题?
原因是在页面中找不到所需的元素。这可能有不同的原因。
1)如果尚未加载带有new ShoppingPage(driver)
的页面片段,则调用"//span[@class='qa708e IYWnmd']"
的时间过早。
2)可能是XPath定义不正确,并且页面上没有这样的元素。检查您的XPath。