簡體   English   中英

Selenium Webdriver-在pagefactory中實現鏈接的webdriver findelement

[英]Selenium Webdriver -Implement chained webdriver findelement in pagefactory

我有一個Webelements列表,其中選擇了一個隨機元素。 現在有一個隨機的Web元素,我需要在其中找到所有子元素。 我如何在pagefactory中實現這一目標。

在硒中,這是有效的:

List<WebElement> listOfElements = driver.findElements(By.xpath("//locator"));

WebElement randomElement = // Code to get a random element out of this list.

String title = randomElement.findElement(By.xpath(".//[@id='title']"));

在頁面工廠中,我嘗試過:

@FindBy(xpath="//locator")

List<WebElement> listOfElements;

@FindBy(id="title");

WebElement title;


WebElement randomElement = // Code to get a random element out of this list.

停留在如何使用頁面工廠注釋在隨機元素中獲取標題的問題。

我知道這會工作:

String title = randomElement.findElement(By.xpath(".//[@id='title']"));

正如我在整個項目中使用頁面工廠注釋一樣,有沒有一種方法可以實現相同目的。

您在頁面對象中創建一個公共方法,該方法返回Webelements列表。

public List<WebElement> getTitles(WebElement randomElement) {
    return randomElement.findElements(by.id("title"));
}

對您來說可能為時已晚,但如果它對其他人有所幫助:可以這樣做,但並不簡單,需要自定義WebDriver的內部類。

我在今年早些時候的博客文章中討論了如何使用“阻止”類解決此問題。 這並不是一件容易的事,但是有一個github項目,其中包含我想使用的代碼。

本質上,我使用每個定位的WebElement生成一個稱為塊的新“ PageObject”類。 只要每個隨機元素都相同,就可以使用PageFactory表示法查找子WebElement。

請注意,所有這些都需要大量開銷,因此,如果您僅嘗試獲取標題,則將流API與鏈式findElements()調用一起使用會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM