D Dev Duniya
Back to Journal
9 min read

How to find an element in Selenium Webdriver | Find Element By ID, Class, Tag_Name, Name, or CSS 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 single data from websites using different methods like using ID, Class, Tag Name, Name, and CSS in selenium web driver. First, we will see how to fetch single data in text form or any link after that in the next tutorial, we will see how to fetch multiple data using a loop. So let's start this tutorial:-
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/selenium-webdrivercommands/")
driver.maximize_window()
driver.title

Selenium has been updated, so we will not use this syntax to find the element "find_element_by_id". Instead, 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 Element By ID method in Selenium

If you want to want to use the ID to fetch any data from any website then you will have to use this syntax.

Syntax:

driver.find_element(By.ID, 'id_of_element')


First, you will have to copy the element id from the webpage after that add the ID at "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 webdriver object. So here is a Complete example to fetch this blog title using ID:-

# 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/selenium-webdrivercommands/')

# Maximizing Window
driver.maximize_window()

# get element text value
title = driver.find_element(By.ID, 'Webdriver_Commands_Description').text

#print title
print("Title of Block= ",title)

# just close the browser
driver.close()

No.2: Find Element By NAME method in Selenium

We can also search any data by using the name element if there is a given name element on your website or blog. As we know that most name element is given in the form input field and with the help of the name element we can search any data in that input field. So if you want to search data using the name, you can see the syntax and an example:-

Syntax:

driver.find_element(By.NAME, 'name_of_element')

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/selenium-webdrivercommands/')

# Maximizing Window
driver.maximize_window()

# enter keyword to search
keyword = "devduniya"

# get element
title = driver.find_element(By.NAME, 'comment')

# 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 elements in a webpage using locating strategies of Selenium Web Driver using "find_element(By.XPATH, 'xpath_of_element')". 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_element(By.XPATH, 'xpath_of_element')

First, you will have to get the XPATH of the element 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/selenium-webdrivercommands/')

# Maximizing Window
driver.maximize_window()

# get element text value
title = driver.find_element(By.XPATH, '//*[@id="post-938"]/h1').text

#print title
print("Title of Article= ", title)

# just close the browser
driver.close()

No.4: Find Element By TAG_NAME method in Selenium

The fourth method to grab elements is BY.TAG_NAME. You can grab or fetch elements with the help of HTML tag_name, mostly it is used for small scrapers where only fewer tags are available. But it is important to know you:

Syntax:

driver.find_element(By.TAG_NAME, 'tagname_of_element')

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/selenium-webdrivercommands/')

# Maximizing Window
driver.maximize_window()

# get element text value
title = driver.find_element(By.TAG_NAME, 'h1').text

#print title
print("Title of Article= ", title)

# just close the browser
driver.close()

No.5: Find Element By CLASS_NAME method in Selenium

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

Syntax:

driver.find_element(By.CLASS_NAME, 'classname_of_element')

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/selenium-webdrivercommands/')

# get element text value
title = driver.find_element(By.CLASS_NAME, 'entry-title entry--item h2').text

# print title
print(title)

# just close the browser
driver.close()

No.6: Find Element By LINK_TEXT method in Selenium

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

Syntax:

driver.find_element(By.LINK_TEXT, 'linktext_of_element')

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

# enter keyword to search
keyword = "devduniya"

driver.get('https://devduniya.com/selenium-webdrivercommands/')

# get link and click on that
title = driver.find_element(By.LINK_TEXT, 'Blogger').click()

# Wait to see response
time.sleep(4)

# 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 Element 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 don't know the complete text of that link then you can simply give some text of that link. To do that read the example:

Syntax:

driver.find_element(By.PARTIAL_LINK_TEXT, 'partial_link_of_element')

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

# enter keyword to search
keyword = "devduniya"

driver.get('https://devduniya.com/selenium-webdrivercommands/')

# get link and click on How To Install
title = driver.find_element(By.PARTIAL_LINK_TEXT, 'Tutor').click()

# Wait to see response
time.sleep(4)

# just close the browser
driver.close()

No.8: Find Element 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 that element that you want to find, after a simple paste of the CSS it will automatically fetch the text and link. So to do that follow the example:

Syntax:

driver.find_element(By.CSS_SELECTOR, 'css_selector_of_element')

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/selenium-webdrivercommands/')

# get link and click on How To Install
title = driver.find_element(By.CSS_SELECTOR, '.h2').text

#Print title
print(title)

# just close the browser
driver.close()

No.9: Find Element 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 the element instead of we pass the full path of the element. You can get the full path of elements by right click on any element and going to copy after that you can see the full path option. Just copy it and paste it in place of xpath_element. Follow the example:

Syntax:

driver.find_element(By.XPATH, 'full_xpath_element')

"/html/body/div[1]/div/div/div/main/div/article/h1" this is the full path of article title that we are going to get. We can page as it but we will paste it in short so to do that just remove some starting tag and add two slashes instead of the full path.

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/selenium-webdrivercommands/')

# get link and click on How To Install
title = driver.find_element(By.XPATH, '//main/div/article/h1').text #we have only given short fullxpath and added two \\.

#Print title
print(title)

# just close the browser
driver.close()

So, guys, we have successfully covered all the selenium driver selectors. We have discussed how to grab 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 How to Find Elements in Selenium Python or Find Many items with the help of Selenium Webdriver.

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 →