Folloz
Folloz

Reputation: 51

size() option is missing using Selenium webdriver through Java

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();
    }
}

Upvotes: 0

Views: 7214

Answers (4)

Sachin
Sachin

Reputation: 1

driver.findElements(By.tagName("a")).size();

It will only display size when you will use findElements not Element.

Upvotes: 0

Ranbeer Rathore
Ranbeer Rathore

Reputation: 1

    ArrayList<WebElement> firstLinkurl = (ArrayList<WebElement>) 

    driver.findElements(By.xpath("write your xpath here"));

    System.out.println(link.size());

    firstLinkurl.get(0).click();//also with this you can also click any link on the page just by providing the index number.

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

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.

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());

Upvotes: 3

Rafał Sokalski
Rafał Sokalski

Reputation: 2007

Use "findElements" instead of "findElement". It returns list of elements so you can iterate through them.

The difference is that findElement returns first matched element and findElements return list of all matched elements

Upvotes: 3

Related Questions