D Dev Duniya
Back to Journal
10 min read

How to Fetch Multiple Data in Selenium Webdriver | Find Multiple Data using Loop in Selenium Webdriver- DevDuniya

Ashish Yadav

Ashish Yadav

Published on Dev Duniya
Keep exploring
Compare the details before you buy.

So hey guys welcome back to our next tutorial, In this tutorial, we are going to see how to fetch multiple data from a website using different methods like using ID, Class, Tag Name, Name, and CSS in selenium web driver. We will see how to fetch multiple data using a loop from a single element. So let's start this tutorial without wasting any time:-
Firstly you will have to import the selenium web driver module after that make an object of the web driver so that you can use functions. To do that:

from selenium import webdriver
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")

Now its time to get the URL of any website from where you want to fetch data, so I have given my own website URL:

driver.get("https://devduniya.com/find-single-element-selenium-webdriver/")
driver.maximize_window()
driver.title

Selenium has been updated, so don't use this syntax "find_element_by_id". Instead of we will use "By. ID" to fetch data, so to do that first you will have to import
one module:

from selenium.webdriver.common.by import By

After that, you can use this type of syntax in your code "By.ID"

No.1: Find Elements By ID method in Selenium

If you want to use ID to fetch multiple data from any web page then you will have to use this syntax.

Syntax:

driver.find_elements(By.ID, 'id_of_elements')

First, you will have to copy the id of the element from the webpage after that add the ID
ad "id_of_element". And at the end of this command please add ".text" because it returns the text value of that id else it returns the selenium web driver object.
So here is a Complete example to fetch a list of items using ID:-

Example:-

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# get url
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# Maximizing Window
driver.maximize_window()
# get elements text value
total_element = driver.find_elements(By.ID, 'menu-top-nav-menu')//for list of elements
print(total_element)
for one_text in total_element:
	print(one_text.text)
driver.implicitly_wait(10)
# just close the browser
driver.close()

No.2: Find Elements By NAME method in Selenium

We can also search any data by using name elements if there is given name element on the webpage with same name input bosx. As we know that most name element is given in the forms input field and with the help of the name element we can search any data in multiple input field. So if you want to search data using name, you can see the syntax and an example:-

Syntax:

driver.find_elements(By.NAME, 'name_of_elements')

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# get url
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# Maximizing Window
driver.maximize_window()
# enter keyword to search
keyword = "devduniya"
# get elements
total_element = driver.find_elements(By.NAME, 'input_field_with_same_name')//for list of elements
print(total_element)
for one_text in total_element:
	print(one_text.text)
# Now you can search given keyword in the input box

No.3: Find Element By XPATH method in Selenium

In this method, we will focus on how to fetch multiple data in a webpage using locating strategies of Selenium Web Driver using "find_elements(By.XPATH, 'xpath_of_elements')".
XPath is the language utilized for finding hubs in an XML archive. As HTML can be an execution of XML (XHTML), Selenium clients can use this strong language to target components in their web applications.

Syntax:

driver.find_elements(By.XPATH, 'xpath_of_elements')//for list of elements

First, you will have to get XPATH of the same elements so to get that, just right-click on that element line and click on a copy after that you can there an XPATH option, just copy it and paste it.

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# get url
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# Maximizing Window
driver.maximize_window()
# get element text value
total_element = driver.find_elements(By.XPATH, '//*[@id="menu-top-nav-menu"]')//for list of elements
print(total_element)
for one_text in total_element:
	print(one_text.text)
driver.implicitly_wait(10)
# just close the browser
driver.close()
# just close the browser
driver.close()

No.4: Find Elements By TAG_NAME method in Selenium

The fourth method to grap elements is BY.TAG_NAME. You can grap or fetch multipl elements with the help of html tag_name, mostly it is used for small scrapers was only a fewer tags available. But it is important to know you:

Syntax:

driver.find_elements(By.TAG_NAME, 'tagname_of_elements')//for list of elements

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# get url
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# Maximizing Window
driver.maximize_window()
# get element text value
total_element = driver.find_elements(By.TAG_NAME, 'li')//for list of elements
print(total_element)
for one_text in total_element:
	print(one_text.text)
driver.implicitly_wait(10)
# just close the browser
driver.close()
# just close the browser
driver.close()

No.5: Find Elements By CLASS_NAME method in Selenium

The fifth method is to grab multiple elements using By.CLASS_NAME. If there is an available unique class name for multiple elements in the web element then we can fetch that element with class names. So just copy the class name from the web element and paste it here:

Syntax:

driver.find_elements(By.CLASS_NAME, 'classname_of_elements')//for list of elements

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# Maximizing Window
driver.maximize_window()
# enter keyword to search
keyword = "devduniya"
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# get element text value
total_element = driver.find_elements(By.CLASS_NAME, 'primary-menu-ul')//for list of elements
print(total_element)
for one_text in total_element:
	print(one_text.text)
driver.implicitly_wait(10)
# just close the browser
driver.close()
# just close the browser
driver.close()

No.6: Find Elements By LINK_TEXT method in Selenium

LINK_TEXT method is used to click at any link or to get text value with the help of their text given. You will have to enter only text of that link, it will automatically find that link after that you can go on that page.

Syntax:

driver.find_elements(By.LINK_TEXT, 'linktext_of_elements')//for list of elements

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# Maximizing Window
driver.maximize_window()
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# get link and click on that
total_element = driver.find_elements(By.LINK_TEXT, 'Programming Blog')//for list of elements
print(total_element)
for one_text in total_element:
	one_text.click()
	driver.back()
	time.sleep(3)
driver.implicitly_wait(10)
# just close the browser
driver.close()
# just close the browser
driver.close()

Here I have used time.sleep(), because when you click on any link it should show you that it has been clicked. If you will not use time.sleep() then it will click but you can not see.

No.7: Find Elements By PARTIAL-LINK_TEXT method in Selenium

LINK_TEXT AND PARTIAL_LINK_TEXT are the same but in the partial_link_text you can give only some text of that link and it will fetch that link. If you dont know complete text of that link then you can simply give some text of that link. To do that read the example:

Syntax:

driver.find_elements(By.PARTIAL_LINK_TEXT, 'partial_link_of_elements')//for list of elements

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# Maximizing Window
driver.maximize_window()
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# get link and click
total_element = driver.find_elements(By.PARTIAL_LINK_TEXT, 'Blogging')//for list of elements
for one_text in total_element:
	one_text.click()
	driver.back()
	time.sleep(3)
driver.implicitly_wait(10)
# just close the browser
driver.close()
# just close the browser
driver.close()

No.8: Find Elements By CSS_SELECTOR method in Selenium

CSS_SELECTOR is used to fetch data or you can just click on the link with the help of this method. In this method, you will have to select the CSS of those elements that you want to find, after that simply paste the CSS it will automatically fetch the multiple texts or links using a loop because there are so many items of the same element. So to do that follow the example:

Syntax:

driver.find_elements(By.CSS_SELECTOR, 'css_selector_of_elements')//for list of elements

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# Maximizing Window
driver.maximize_window()
# enter keyword to search
keyword = "devduniya"
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# get link and click on How To Install
total_element = driver.find_elements(By.CSS_SELECTOR, '.nav-menu-desktop')//for list of elements
print(total_element)
for one_text in total_element:
	print(one_text.text)
driver.implicitly_wait(10)
# just close the browser
driver.close()
# just close the browser
driver.close()

No.9: Find Elements By FULL_XPATH method in Selenium

This is not a method, it is the same as XPATH but the main difference is that here we will not pass the XPath of elements instead of we pass fullxpath of the elements. You can get fullxpath of elements by right click on any element and going to copy after that you can see fullxpath option. Just copy it and paste it in place of xpath_element. Follow the example:

"/html/body/div[1]/header/div/div[2]/div/div/div[1]/div/div[2]/div[1]/nav/ul" this is the fullxpath of the list of element that we are going to fetch. We can pass it as it but we will give it in short so to do that just remove some starting tags and add two slashes instead of the full path like "//div[1]/div/div[2]/div[1]/nav/ul".

Syntax:

driver.find_elements(By.XPATH, 'full_xpath_element')//for list of elements

Example:

# importing Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
# create webdriver object
driver = webdriver.Chrome(executable_path="C://Users//Ashish//Desktop//Selenium//chromedriver.exe")
# Maximizing Window
driver.maximize_window()
# enter keyword to search
keyword = "devduniya"
driver.get('https://devduniya.com/find-single-element-selenium-webdriver/')
# get link and click on How To Install
total_element = driver.find_elements(By.XPATH, '//div[1]/div/div[2]/div[1]/nav/ul/li')//for list of elements
print(total_element)
for one_text in total_element:
	print(one_text.text)
driver.implicitly_wait(10)
# just close the browser
driver.close()
# just close the browser
driver.close()

So guys, we have successfully covered all the selenium driver selectors in python programming. We have discussed how to grab multiple elements using ID, Class name, Tag name, Name, Link text, Partial link text, CSS selector and Full xpath. In the next tutorial, we have covered some advanced concepts of Selenium Webdriver using python programming.

Keep reading

12 latest articles

The newest stories from Dev Duniya, excluding the article you are reading.

All articles →

From the product catalog

Explore device specifications

Specification cards update automatically from published catalog products, grouped by brand.

Apple specifications

Explore Apple devices →
Apple Apple MacBook Pro 14-inch
Apple Apple MacBook Pro 14-inch specifications

Experience unrivaled performance with the latest Apple MacBook Pro, available in 14-inch and 16-inch models powered by the revolutionary M6, M5 Pro, and M5 Max chips. Built for Apple Intelligence, these powerhouse laptops feature advanced Thunderbolt 5 ports, Wi-Fi 7, and optional Nano-texture displays for minimal glare. Maximize your creative potential with up to 128GB of unified memory in a sleek Space Black or Silver finish.

Display size
14-inch
Screen Type
Standard display (low reflectivity) or Nano-texture display (reduces reflections and glare in bright lighting)
External Display Support
Support for up to three external displays (M5 Pro) / Support for up to four external displays (M5 Max)
M6 Chip
12‑core CPU, 12‑core GPU, Dual 16‑core Neural Engine
See all specifications →
Apple Apple MacBook Pro 16-inch
Apple Apple MacBook Pro 16-inch specifications

Experience unrivaled performance with the latest Apple MacBook Pro, available in 14-inch and 16-inch models powered by the revolutionary M6, M5 Pro, and M5 Max chips. Built for Apple Intelligence, these powerhouse laptops feature advanced Thunderbolt 5 ports, Wi-Fi 7, and optional Nano-texture displays for minimal glare. Maximize your creative potential with up to 128GB of unified memory in a sleek Space Black or Silver finish.

Display size
16-inch
Screen Type
Standard display (low reflectivity) or Nano-texture display (reduces reflections and glare in bright lighting)
External Display Support
Support for up to three external displays (M5 Pro) Support for up to four external displays (M5 Max)
M5 Pro Chip
18‑core CPU, 20‑core GPU, 16‑core Neural Engine
See all specifications →
Apple Apple iPhone Duo
Apple Apple iPhone Duo specifications

Apple iPhone Duo is a new-generation iPhone model focused on a sleek design, powerful performance, and everyday usability. Explore iPhone Duo specifications, features, display, cameras, battery, and expected price in the USA. Get the latest information about Apple iPhone Duo and its key features. Compare iPhone Duo with other popular Apple iPhone models.

Size (inches)
Open: 4.64 x 6.48 x 0.21 Closed: 4.64 x 3.31 x 0.44
Weight (ounces)
8.96
Display size (inches)
Inner: 7.6-inch (diagonal) all-screen OLED folding display Outer: 5.4-inch (diagonal) all-screen OLED display
Resolution (pixels)
Inner: 1878-by-2670-pixel resolution at 430 ppi Outer: 1398-by-2034-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro Max
Apple Apple iPhone 18 Pro Max specifications

Apple iPhone 18 Pro Max is a high-end flagship smartphone designed for powerful performance and advanced photography. Explore iPhone 18 Pro Max specifications, features, display, cameras, battery, and expected price in the USA. Discover the latest upgrades and key features of Apple’s largest Pro iPhone. Compare iPhone 18 Pro Max with other premium flagship smartphones.

Size (inches)
6.43 x 3.07 x 0.34
Weight (ounces)
8.78
Display size (inches)
6.9-inch (diagonal) all-screen OLED display
Resolution (pixels)
2868-by-1320-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro
Apple Apple iPhone 18 Pro specifications

Apple iPhone 18 Pro is a premium flagship smartphone with advanced performance and an upgraded camera system. Explore iPhone 18 Pro specifications, features, display, cameras, battery, and expected price in the USA. Get the latest details about Apple’s new Pro model and its key upgrades. Compare iPhone 18 Pro features with other flagship smartphones.

Size (inches)
5.91 x 2.83 x 0.34
Weight (ounces)
7.44
Display size (inches)
6.3-inch (diagonal) all-screen OLED display
Resolution (pixels)
2622-by-1206-pixel resolution at 460 ppi
See all specifications →

Google specifications

Explore Google devices →
Google Google Pixel 12
Google Google Pixel 12 specifications

Upgrade to the Google Pixel 12, an advanced 5G AI smartphone. Experience seamless performance powered by the custom Google Tensor G7 processor. Enjoy a stunning 6.3-inch OLED Actua display and take flawless photos with the powerful 50MP dual-camera system. Built with an adaptive all-day battery and seamless Google Gemini AI integration, it is the perfect unlocked device for everyday brilliance.

Size (inches)
6.02 x 2.83 x 0.34
Weight (ounces)
6.95
Display size (inches)
6.3-inch (diagonal) OLED display
Resolution (pixels)
1080 x 2424 pixels
See all specifications →
Google Google Pixel 12 Pro
Google Google Pixel 12 Pro specifications

Discover the new Google Pixel 12 Pro, the ultimate 5G AI smartphone. Powered by the next-generation Google Tensor G7 chip and built-in Gemini AI, it delivers blazing-fast performance. Capture professional-grade photos with the advanced 50MP Pro Triple Camera system featuring 5x optical zoom. Complete with a stunning 6.3-inch Super Actua display, all-day adaptive battery, and a premium titanium frame. Upgrade to the smartest Android phone today.

Size (inches)
6.02 x 2.83 x 0.33
Weight (ounces)
7.3
Display size (inches)
6.31-inch LTPO OLED display
Resolution (pixels)
2856 x 1280 pixels
See all specifications →
Google Google Pixel 12 Pro XL
Google Google Pixel 12 Pro XL specifications

Go bigger with the Google Pixel 12 Pro XL, the ultimate large-screen 5G AI smartphone. It boasts a massive 6.8-inch OLED display and the lightning-fast Google Tensor G7 chip. Capture master-level photography using the 50MP Pro Triple Camera setup. Equipped with a massive 5500mAh battery for extended endurance and fast 67W charging, this unlocked flagship redefines premium power.

Size (inches)
6.43 x 3.07 x 0.34
Weight (ounces)
8.18
Display size (inches)
6.8-inch LTPO OLED Super Actua display
Resolution (pixels)
2992 x 1344 pixels
See all specifications →
Google Google Pixel 12 Pro Fold
Google Google Pixel 12 Pro Fold specifications

Transform your mobile experience with the Google Pixel 12 Pro Fold. This unlocked 5G AI foldable smartphone unfolds into a gorgeous 8-inch dual-UTG inner OLED display for next-level multitasking and entertainment. Powered by the Google Tensor G7 chip, it features flexible triple-camera photography, advanced Gemini AI, and a highly durable folding hinge design.

Size (inches) - Unfolded
6.24 x 5.64 x 0.16
Size (inches) - Folded
6.24 x 2.87 x 0.35
Weight (ounces)
7.58
Main Display
8.0-inch Foldable LTPO OLED, 120Hz
See all specifications →

Samsung specifications

Explore Samsung devices →