Python – find_element_by_id() method in Selenium

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_by_id() method is used. Id is basically unique attribute assigned to the elemnts of the we page such as buttons, image, heading etc.

Syntax :

driver.find_element_by_id(ID)

Argument :



Takes ID in string format

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”

filter_none

edit
close

play_arrow

link
brightness_4
code

#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_by_id('geek_id')

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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_by_id('scrollTopBtn')

chevron_right


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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.