- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to count the total number of links in Selenium with python?
We can count the total number of links in a page in Selenium with the help of find_elements method. While working on links, we will always find the tagname in the html code and its value should be anchor (a).
This characteristic is only applicable to links on that particular page and to no other types of UI elements like edit box, radio buttons and so on.
To retrieve all the elements with the tagname as anchor we will use find_elements_by_tag_name() method. This method returns a list of web elements with the type of tagname specified in the method argument. In case there are no matching elements, an empty list will be returned.
After the list of links is fetched, in order to count its total numbers, we need to get the size of that list. The size of the list can be obtained from the len() method of the list data structure.
Finally this length is printed on the console.
Syntax
driver.find_elements_by_tag_name("a")
Example
Code Implementation to count links.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/index.htm") #to refresh the browser driver.refresh() #to get the list of links present on the web page l = driver.find_elements_by_tag_name('a') #print the count with the len method on console print(len(l)) #to close the browser driver.quit()
- Related Articles
- How to count the total number of links in a page in Selenium?
- How to count the total number of frames in Selenium with python?
- How to count the total number of tables in a page in Selenium with python?
- How to count the total number of radio buttons in a page in Selenium with python?
- How to count the number of checkboxes in a page in Selenium with python?
- How to count the number of rows in a table in Selenium with python?
- How to count total number of occurrences of an object in a Python list?
- How to count the number of occurrences of a particular text inside a table in a page in Selenium with python?
- How to count the number of frames in a page in Selenium?
- How to count the total number of lines in the file in PowerShell?
- How to get the total number of checkboxes in a page using Selenium?
- How to count the total number of frames in OpenCV using C++?
- How to show the number of links in a document with JavaScript?
- How to count the number of headers in a web table in Selenium?
- Write a python program to count total bits in a number?
