Count total number of Links In Webpage Using Selenium In Python
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python.
Installation
1.1 Selenium Bindings in Python
Selenium Python bindings provide a convenient API to access Selenium Web Driver like Firefox,Chrome,etc.
Pip install Selenium
1.2 Web Drivers
Selenium requires a web driver to interface with the chosen browser. Web drivers is a package to interact with a web browser. It interacts with the web browser or a remote web server through a wire protocol which is common to all. You can check out and install the web drivers of your browser choice.
Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads Firefox: https://github.com/mozilla/geckodriver/releases Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/
Step-by-step Approach:
- Import required modules
Python3
# import modules from selenium import webdriver from selenium.webdriver.common.by import By import time |
- Taking any URL.
Python3
# assign web page url |
- using By.TAG_NAME find web link in webpage.
Implementation:
Python3
#import modulefrom selenium import webdriverfrom selenium.webdriver.common.by import By driver = webdriver.Chrome() # url # find web linkslink = driver.find_elements(By.TAG_NAME, 'a') # using len function count how many linksprint(len(link)) |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course



