Open In App
Related Articles

Python – find_element() method in Selenium

Improve Article
Improve
Save Article
Save
Like Article
Like

While performing any action on a web page using selenium, there is need of locators to perform specific tasks. Locators in web page are used to identify unique elements within a webpage. Web elements could be anything that the user sees on the page, such as title, table, links, buttons, toggle button, or any other HTML element.

In order to find element by “id” find_element() method is used. Id is basically unique attribute assigned to the elements of the web page such as buttons, image, heading etc.

Syntax :  

driver.find_element("id","id_of_element")

Argument:  

First argument: Takes id in string format
Second argument: Takes id of the element to be located 

Note: The first element with the “id” attribute value matching the location will be returned. If no element has a matching “id” attribute, a NoSuchElementException will be raised.

Example 1:

Consider the following page source:  

Below is the code for finding the element i.e “geek_id” 

Python3




# importing webdriver from selenium
from selenium import webdriver
 
# Here Chrome  will be used
driver = webdriver.Chrome()
 
# Opening the website
driver.get(url)
 
# finds button using its id
form = driver.find_element("id", 'geek_id')


The form element can be located like this.

Example: Source code of https://www.geeksforgeeks.org/ is given below. 

Below is the code for finding the scroll button with the help of its ID i.e scrollTopBtn.  

Python3




# importing webdriver from selenium
from selenium import webdriver
 
# Here Chrome  will be used
driver = webdriver.Chrome()
 
# URL of website
 
# Opening the website
driver.get(url)
 
# finds button using its id
bt = driver.find_element("id", 'scrollTopBtn')


With this code we can locate the scroll top button of this site.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 Sep, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials