Java 获取Selenium中页面对象元素的Null值
我在一个cucumber、TestNG和Selenium项目中工作,我的PageObject页面如下所示Java 获取Selenium中页面对象元素的Null值,java,selenium,pageobjects,Java,Selenium,Pageobjects,我在一个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
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;
}
}
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());
}
}
}
我的黄瓜步骤定义是
@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());
}
}
@When(“搜索小于{int}的产品”)
小于(整数int1)的产品的公共无效搜索{
ShoppingPage myshopping=新的ShoppingPage(驱动程序);
List SearchResults=myshopping.getSearchResult();
for(SearchResultComponent myResult:SearchResults){
System.out.println(myResult.getProductName());
}
}
问题陈述:
当我试图在步骤定义中获取getSearchResult()时,出现了空点错误。不知道为什么会想到如何解决这个问题 原因是在页面中找不到所需的元素。这可能有不同的原因 1) 当带有
“//span[@class='qa708e IYWnmd']”
的页面片段尚未加载时,您调用new ShoppingPage(driver)
太早了
2) 可能是XPath定义不正确,页面上没有此类元素。检查您的XPath。在Shoppingpage类中使用
WebElement
作为类型,而不是下面提到的类名SearchResultComponent
@FindBy(xpath="//div[@class='sh-dlr__list-result']")
private List<WebElement> SearchResult;
public List<WebElement> getSearchResult() {
return SearchResult;
}
@FindBy(xpath=“//div[@class='sh-dlr\uu list-result']))
私有列表搜索结果;
公共列表getSearchResult(){
返回搜索结果;
}
在cucumber步骤定义中也使用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());
}
}
@When(“搜索小于{int}的产品”)
小于(整数int1)的产品的公共静态无效搜索{
ShoppingPage myshopping=新的ShoppingPage(驱动程序);
List SearchResults=myshopping.getSearchResult();
for(WebElement myResult:SearchResults){
System.out.println(myResult.getText());
}
}
谢谢@Yosuva。我将其更新为WebElement,其工作方式如下
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;
}
}
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());
}
}
}
public void search\u查找小于(整数int1)的产品{
ShoppingPage myshopping=新的ShoppingPage(驱动程序);
SearchResultComponent resultcomp=新的SearchResultComponent(驱动程序);
List PriceResult=resultcomp.getProductPrice();
List ProductName=resultcomp.getProductName();
对于(int i=0;i您确定异常发生在myshopping.getSearchResult()
中吗?不在for
循环中?列出SearchResults=myshopping.getSearchResult());对于SearchResults本身,我得到的是null值,在传递到For循环时,这是这个null点错误的原因。我会在PageFactory.initElements(驱动程序,这个)上逐步检查您的代码;
。可能是因为找不到它们而没有初始化?您好,谢谢您的更新。我了解原因,但不确定如何通过将搜索结果作为要迭代的组件传递来单独获取产品名称。我已经交叉检查了xpath及其仅引用了正确的元素。因此,它不应该是一个问题问题我猜没有什么神奇之处。如果属性为null,则表示页面上没有此类元素。您可以通过driver.findElements(by.xpath(“…”)
进行检查。在调用new ShoppingPage(driver)之前临时添加此类检查
。如果此直接搜索也返回null,它将确认XPath错误或元素确实不存在。