我确实在这里遇到了这个问题,但根据该线程,“错误消失了,我不知道它是如何修复的”... got error while trying to use @FindBy annotation in selenium
这是我写的代码。奇怪的是我已经在页面对象类上定义了另一个 Web 元素,并且它工作正常。我似乎不明白问题出在哪里。我刚刚学习selenium,以前从未遇到过这个问题。我尝试在网络上搜索答案,但似乎找不到此特定问题的答案。
这是页面对象类上的第一个 Web 元素,它工作正常。
// Profile button element
@FindBy(xpath="//div[@class='container']//nav//li[2]//a[1]")
@CacheLookup // is used to improve the performance
static WebElement profileBtn;
使用此元素(在同一页面对象类上),我得到“该位置不允许使用注释 @FindBy”。我已尝试重新启动 Eclipse 并清理项目,但它不允许第二个元素。
@FindBy(xpath="//div[@id='stateDropdown-styler']//div[@class='jq-selectbox__trigger']")
@CacheLookup // is used to improve the performance
WebElement clickProvinceDropDownArrow()
最佳答案
请参阅definition of @FindBy annotation 。它仅适用于字段和类,不适用于方法。它减少了您需要编写的代码量来定位元素并将其存储到成员变量中。
如果您使用页面对象模式,则使用此注释定义成员变量。如果您想让此元素在页面对象之外可用,请实现一个返回此成员变量的 getter 方法。
@FindBy(xpath="//div[@id='stateDropdown-styler']//div[@class='jq-selectbox__trigger']")
private WebElement clickProvinceDropDownArrow;
public WebElement getClickProvinceDropDownArrow() {
return clickProvinceDropDownArrow;
}
关于java - 尝试在 selenium 中使用 @FindBy 注释时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57960959/