Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获得WebElement的数量计数_Java_Selenium - Fatal编程技术网

Java 如何获得WebElement的数量计数

Java 如何获得WebElement的数量计数,java,selenium,Java,Selenium,有没有一种方法可以通过WebElement查找元素而不使用 原因是我想找到“testElement”的所有元素,但我不确定如何对其进行编码,因为它需要的是By而不是WebElement。我希望有一个通过WebElement的方法 示例代码如下: @FindBy(id = "test") public WebElement testElement; 要获得该尺寸,我将尝试以下方法,但它失败了,正如预期的那样: List<WebElement> testCount = driver()

有没有一种方法可以通过WebElement查找元素而不使用

原因是我想找到“testElement”的所有元素,但我不确定如何对其进行编码,因为它需要的是By而不是WebElement。我希望有一个通过WebElement的方法

示例代码如下:

@FindBy(id = "test")
public WebElement testElement;
要获得该尺寸,我将尝试以下方法,但它失败了,正如预期的那样:

List<WebElement> testCount = driver().findElements(elementPage.testElement);
        Assert.assertEquals(testCount.size(), 5);
List testCount=driver().findElements(elementPage.testElement);
Assert.assertEquals(testCount.size(),5);

您将其声明为单个Web元素, 您应该将其声明为列表,如下所示:

@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());
List testElement=driver.findElements(By.id(“Test”);
System.out.println(“元素计数:+testElement.size());

希望有帮助。

什么是
elementPage
?我认为它是pageObjectYeah是pageObjectHi Valga,不,这不起作用,因为它无法识别。size()奇怪,应该识别list.size(),因为它是默认方法,您是否将变量类型更改为list?哦,不,对不起,我错过了,您将其声明为list