有没有办法能够通过 WebElement 查找元素而不使用 By?
原因是我想找到“testElement”的所有元素,但我不确定如何编码它,因为它需要 By 而不是 WebElement。我希望有一种通过 WebElement 的方法。
示例代码如下:
@FindBy(id = "test")
public WebElement testElement;
为了获得大小,我会尝试以下方法,但它失败了,因为它需要一个By:
List<WebElement> testCount = driver().findElements(elementPage.testElement);
Assert.assertEquals(testCount.size(), 5);
最佳答案
您将其声明为单个网络元素, 您应该将其声明为列表,如下所示:
@FindBy(id = "test")
public List<WebElement> testElement;
然后你可以在你的代码中使用它,如下所示:
Assert.assertEquals(elementPage.testElement.size(), 5);
你也可以直接尝试,不使用elementPage:
List<WebElement> testElement= driver.findElements(By.id("Test"));
System.out.println("Element count: " + testElement.size());
希望有帮助。
关于java - 如何获取 WebElement 的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222910/