【问题标题】:How to use @FindBy for driver.findElements(By.xpath()).size>0如何将@FindBy 用于 driver.findElements(By.xpath()).size>0
【发布时间】:2017-04-20 16:36:27
【问题描述】:

目前我正在为一个元素使用@FindBy 注释,如下所示

@FindBy(xpath = "//a[@class='fNiv' and contains(text(), 'Home')]")
public static WebElement Tab_Home;

但是当我尝试使用布尔值时,我收到一个错误“更改为 getSize()”,而我的布尔语法是

//AllGeneralTabs is the class name where I have stored my elements
Boolean home=AllGeneralTabs.Tab_Home.size()>0;

当我将我的元素定义为时,我希望某些东西以这种方式工作

public static final By Tab_Home=By.xpath("//a[@class='fNiv' and contains(text(), 'Home')]");

那么布尔值将完美地适用于以下语法

Boolean home=driver.findElements(AllGeneralTabs.Tab_Home).size()>0;

它返回 true/False 并按预期工作,但 @FindBy 注释不会发生这种情况。由于我现在使用 @FindBy 定义了所有元素,并且无法返回并将其更改为最终语句,因此我没有时间。

【问题讨论】:

标签: selenium-webdriver


【解决方案1】:

尝试以下解决方案。

@FindAll(@FindBy(how = How.XPATH, using = "//a[@class='fNiv' and contains(text(), 'Home')]"))
List<WebElement> allElements;

@FindBys(@FindBy(xpath="//a[@class='fNiv' and contains(text(), 'Home')]")))
List<WebElement> allElements;

让我知道它是否适合你。

【讨论】:

  • 谢谢...现在可以无任何错误地识别元素
  • "how = How.XPATH," 这是做什么的?我正在尝试查找在线资源,但发现它们令人困惑。
【解决方案2】:

你在WebElementsize女巫回归DimensionListsize女巫回归int之间混合。你没有说你到底想做什么,但似乎你正在寻找

@FindBy(xpath = "//a[@class='fNiv' and contains(text(), 'Home')]")
public static List<WebElement> Tab_Home;

【讨论】:

  • 是的,我正在寻找这个。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多