[英]Selenium can't find element by @FindBys annotation in case of two conditions
我在頁面上有一個帶有屬性的文本元素:
我正在嘗試使用 FindBys 注釋獲取此元素:
@FindBys({@FindBy(tagName = "h5"),@FindBy(id = "email_label")})
private WebElement EmailLabel;
但出現錯誤:org.openqa.selenium.NoSuchElementException:無法使用 By.chained({By.tagName: h5,By.id: email_label}) 定位元素
同時,如果僅使用一種條件,則可以正確找到元素:
@FindBys({@FindBy(id = "email_label")})
private WebElement EmailLabel;
或者
@FindBys({@FindBy(tagName = "h5")})
private WebElement EmailLabel;
我用:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>
FindBys用來標記上的場pageobjects表明,查找應使用一系列@FindBy
中描述標簽鏈ByChained
。 舉個例子:
@FindBys({@FindBy(id = "foo"),
@FindBy(className = "bar")})
ByChained機制用於使用一系列其他查找來定位文檔中的元素。 該類將按順序查找與每個定位器匹配的所有 DOM 元素,Asan 示例:
driver.findElements(new ByChained(by1, by2))
上面的代碼線會發現匹配的所有元素by2
和元件下出現在符合by1
。 所以基本上你不應該在同一個節點上同時使用by
,而是一個by
用於父節點,另一個用於子節點,你可以使用以下Locator Strategy :
@FindBys({@FindBy(tagName = "h5"), @FindBy(tagName = "strong")})
private WebElement EmailLabel;
理想情況下,將<span>
與文本定位為您的電子郵件地址是什么? 您可以使用以下任一定位器策略:
使用css :
@FindBys({@FindBy(css = "h5#email_label>strong"), @FindBy(tagName = "span")}) private WebElement EmailLabel;
使用xpath :
@FindBys({@FindBy(xpath = "//h5[@id='email_label']/strong"), @FindBy(xpath = "//span[contains(., 'your email address')]")}) private WebElement EmailLabel;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.