How to get all the links in webpage in Selenium Python?

Get all the Links in Webpage in Selenium Python

To get all the links present in the webpage using Selenium in Python, call find_elements() method on the webdriver object, and pass By.TAG_NAME as selector, and “a” for value.

driver.find_elements(By.TAG_NAME, 'a')

The above method call returns all the link elements in the webpage as a list, List[WebElement] object.


In this example, we shall consider loading the HTML file at path . The contents of this HTML file is given below.

  <p>Hello World! Read <a id="about_link" href="/tmp/selenium/index-31-about.html">About Hello World</a>.</p>
  <p>Latest Articles</p>
    <li><a id="new_article" href="/tmp/selenium/index-31-new-article.html">About New Article</a></li>
How to get all the links in webpage in Selenium Python?

Now, we shall get all the links (anchor elements) with the tag name as a. And print them to standard output.

Python Program (Selenium)

from selenium import webdriver
from import Service as ChromeService
from import ChromeDriverManager
from import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url

# Find all link elements
links = driver.find_elements(By.TAG_NAME, 'a')

# Iterate over link elements
for link in links:

# Close the browser


<a id="about_link" href="/tmp/selenium/index-31-about.html">Hello World</a>
<a id="new_article" href="/tmp/selenium/index-31-new-article.html">New Article 1</a>


In this Python Selenium tutorial, we learned how to get all the HTML link elements in a webpage, using find_elements() method in Selenium.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is the method used to interact with web elements in Selenium Python?

Not answered

Q3. Which of the following is not a commonly used assertion method in Selenium Python?

Not answered

Q4. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍