我有一個搜索結果頁面,其中包含許多作爲搜索結果生成的產品項目。我可以得到產品的清單。 但是這些webelements內部有產品細節,例如價格,折扣,運費價格等子元素。如何使用頁面對象工廠模型通過使用@FindBy批註自動獲取這些子元素的值。如何使用頁面對象工廠框架爲webelement的子元素
目前我不知道如何顯式地通過在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();
......
}
感謝您的建議。我面臨的問題是我無法唯一識別即時生成的搜索結果產品項目。那麼我怎樣才能唯一標識搜索結果產品項目的子webelement?所以我正在尋找其他可以使用POF @FindBy註釋自動設置子元素值的其他東西。我在http://stackoverflow.com/questions/24251739/selenium-webdriver-page-factory-initialization-using-paths-relative-to-other-el?rq=1 – win