[英]Selenium check element presence if using FindBy annotation
嘗試使用@FindBy
注釋時,我的感覺是沒有完美的方法來檢查元素的存在。
@FindBy (id = "abc")
private WebElement TestElement;
if (TestElement.isPresent) {...};
我用@FindBys
找到了解決方案,但是這樣我必須為每個元素實現一個額外的@FindBys
來檢查 DOM 中的存在? 不是很好。
每個帶有ExpectedConditions.presenceOf...
替代解決方案或使用FindElement
任何解決方案都需要一個定位器作為參數而不是 WebElement,對嗎?
當然,我可以在 try/catch 中使用例如ExpectedConditions.elementToBeClickable(WebElement)
構建變通方法並引發org.junit.jupiter.api.Assertions.fail
,但這不是真正的 DOM 檢查。
對我來說,我似乎更靈活地使用頁面對象的經典By
定義(例如private By searchInput = By.xpath("\\\\input[@class='input_search']");
)而不是使用@FindBy
。 使用 @FindBy 我總是處理 WebElement 本身,之前沒有機會檢查是否存在,對吧?
使用@FindBy
檢查頁面對象上下文中元素的 DOM 存在的最佳實踐解決方案是什么? 或者我應該和By
呆在一起以確保安全。
這可能不是最好的解決方案,但您可以使用 findBy 並返回一個列表:
List<WebElement> TestElement = driver.findElements(By.id(...));
並每次(在 while 循環中)檢查列表 elelement 的大小以查看它是否不為空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.