0

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

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

0
    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.

1 Comment

Also, any explanation added to your post would be welcome.
0
driver.findElements(By.tagName("a")).size();

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.