【发布时间】:2016-08-17 03:15:31
【问题描述】:
我有一个搜索结果页面,其中包含许多作为搜索结果生成的产品项目。我可以得到产品项目的列表。 但是在这些 web 元素中作为子元素包含价格、折扣、运费等产品详细信息。如何使用页面对象工厂模型通过 @FindBy 注释自动获取这些子元素的值。
目前我不知道如何通过在 findElement(By) 函数中显式提供父 WebElement 的上下文而不是 POF 的自动机制来显式获取它们。问题是如何将上下文作为 WebElement 而不是 PageObject 类的驱动程序对象。我找不到任何可以理解的网络文章来解释如何做到这一点。有人可以用一个例子来解释一个简单的方法来做到这一点......我真的很感激。 下面是解释测试类和代表产品的类的代码。
public class TestProducts {
.....
//I can get the list of products from a
List<WebElement> we_prod_box_list = driver.findElements(By.xpath("//div[@id='content']/descendant::div[starts-with(@class,'product_box_container')]"));
.....
}
public class ProductItem {
private WebDriver driver = null;
private Actions action = null;
private WebElement we_prod_box = null;
public String we_prod_box_title = "";
public WebElement we_pt = null;
public String slideImgTitle = "";
public String oldPrice = "";
public String oldPriceTitle = "";
public String subPrice = "";
public WebElement we_purch_disc = null;
private WebDriverWait wait = null;
public String shippingText = "";
public String shippingCost = "";
public String discPrice = "";
.....
we_pt = we_prod_box.findElement(By.xpath(".//div[contains(@class, 'subcategor_price_tag')]"));
slideImgTitle = we_pt.findElement(By.xpath(".//div[contains(@class, 'subcategor_slideimgtitle')]")).getText();
oldPrice = we_prod_box.findElement(By.xpath(".//div[contains(@class, 'oldprice')]")).getText();
oldPriceTitle = we_prod_box.findElement(By.xpath(".//div[contains(@class, 'oldprice')]")).getAttribute("title");
subPrice = we_prod_box.findElement(By.xpath(".//span[contains(@class, 'sub-price')]")).getText();
......
}
【问题讨论】:
标签: java selenium selenium-webdriver webdriver pageobjects