通过Java使用Selenium Webdriver缺少size()选项 [英] size() option is missing using Selenium webdriver through Java
问题描述
一直在遵循一些课程,以提高Selenium Webdrive的自动化技能.尝试计算页面内链接的数量时,没有 size()
方法作为选项.
have been following some classes to improve my automation skills with Selenium Webdrive. I don't have size()
method as an option when trying to count the numbers of links inside a page.
我想念一些罐子吗?导入库?
Am I missing some jars? Import libraries?
java public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Users/Follo/Dropbox/Chrome Drivers/chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
// options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver.get("URL");
WebElement link = driver.findElement(By.tagName("a"));
link.size()
// .size do not appear as an option in the dropdown menu
System.out.println(link.size());
driver.quit();
}
}
推荐答案
size()
size()方法Java中的List接口用于获取此列表中的元素数.也就是说,此方法返回此列表容器中存在的元素数.
size()
The size() method of List interface in Java is used to get the number of elements in this list. That is, this method returns the count of elements present in this list container.
因此,本质上,类型为 WebElement
的变量 link 不支持 size()
方法.因此,您必须将变量 link 的类型更改为 List
,并使用 findElements()
方法填充 List 您可以使用以下解决方案:
So essentially, the variable link which is of type WebElement
won't support the size()
method. So you have to change the type of the variable link as List
and populate the List using findElements()
method and you can use the following solution:
List<WebElement> link = driver.findElements(By.tagName("a"));
System.out.println(link.size());
这篇关于通过Java使用Selenium Webdriver缺少size()选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!