Selenium is a portable framework for testing web applications. Selenium provides a playback tool for authoring functional tests without the need to learn a test scripting language. Before Going Ahead Please refer to this page if you had not installed Selenium. This article revolves around Locators in Selenium and various strategies.
Contents
Webdriver
Selenium WebDriver drives a browser natively, as a real user would, either locally or on remote machines. To start a web browser python selenium Module Needs webdriver. One can download it from here for chromium browserÂ
Python3
# Python program to demonstrate
# Webdriver For Firefox
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://mbasic.facebook.com")
How to Use Other Webdriver
Python3
# Firefox
driver = webdriver.Firefox()
# Google Chrome
driver = webdriver.Chrome()
Getting Source of a Web-Page/URL
By Using this one can get the complete page source/code of current opened URL/Web-Page.Â
Python3
# Python program Continued
# Webdriver For Firefox
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://mbasic.facebook.com")
html = driver.page_source # Getting Source of Current URL / Web-Page Loaded
print(html)
# End
Output: 
One Need to Go Through the Source Code Of the Page To Create Selenium Automated Script
For Example :- 
Locating Elements By :-
1. Id
HTML Elements have some attribute "id" which can be used to Locate Those Elements. For Example :- Finding Input Box For Email FieldÂ
Python3
# Python program Continued
# Finding Input Box For Email Field
# Go Through the Screen Shot Above or Page Source
driver.find_element_by_id("m_login_email")
# End
2. Name
HTML Elements Have attribute "name" associated with them which can be used to Locate Those Elements. For Example :- Finding Input Box For Password FieldÂ
Python3
# Python program Continued
# Finding Input Box For Password Field
# Go Through the Screen Shot Above or Page Source
driver.find_element_by_name("pass")
# End
3. Link Text
HTML Elements which are actually a Link To another Page can be Located using that particular Link Text. For Example :- Finding Forgotten Password Link FieldÂ
Python3
# Python program Continued
# Finding Forgotten Password Link Field
driver.find_element_by_link_text("Forgotten password?")
# End
4. Partial Link Text
HTML Elements which are actually a Link To another Page can be Located using that particular Partial Link Text. For Example :- Finding Forgotten Password Link FieldÂ
Python3
# Python program Continued
# Finding Forgotten Password Link Field
driver.find_element_by_partial_link_text("Forgotten password?")
# End
5. XPath
HTML Elements can be Easily Found by using this For Example :- Find Email and Password Input FieldÂ
Python3
# Python program Continued
# Creating a Reference of Form For Finding Email and Password
# Reference for Form Finding
form = driver.find_element_by_xpath("//form[@id ='login_form']")
# Email
email = form.find_element_by_name("email")
# Password
password = form.find_element_by_name("pass")
# End
6. Tag Name
HTML Elements can be Easily Found by using Tag Name For Example :- Finding Elements by using tag name like Title, Head, Body, Html, a, div, etc.Â
Python3
# Python program Continued
# Finding Title of Facebook Login Page
# Output will be "Facebook - log in or sign up"
title = driver.find_element_by_tag_name("title")
print(title)
# End
7. CSS Selector
HTML Elements can be Easily Found by using CSS For Example :- Finding Elements by using Class, style, etc.Â
Python3
# Python program Continued
# Finding Password Input Field Using Class Name "bl bm bo bp"
password = driver.find_element_by_css_selector('input.bl.bm.bo.bp')
# End
8. Class Name
HTML Elements can be Easily Found by using Class name For Example :- Finding Elements by using Class Name.Â
Python3
# Python program Continued
# Finding Password Input Field Using Class Name "bl bm bo bp"
password = driver.find_element_by_class_name('bl bm bo bp')
# End
9. Entering Input in an Input Field Using Selenium Script
It is Used to Insert Input into Input Field Using inbuilt Function send_keys.Â
Python3
# Python program Continued
# Creating a Reference of Form For Finding Email and Password
# Reference for Form Finding
form = driver.find_element_by_xpath("//form[@id ='login_form']")
email = form.find_element_by_name("email")
password = form.find_element_by_name("pass")
# Inserting("abc@gmail.com") in the Email Input Field
email.send_keys("singh.swaraj1999@gmail.com")
# Inserting("Your Facebook Password") in the Password Input Field
password.send_keys("Your Facebook Password")
# End
10. Locating Submit Button
It is Used to Locate the Submit Button Which is used to submit a formÂ
Python3
# Python program Continued
# Creating a Reference of Form For Finding Email and Password
form = driver.find_element_by_xpath("//form[@id ='login_form']")
email = form.find_element_by_name("email")
password = form.find_element_by_name("pass")
email.send_keys("singh.swaraj1999@gmail.com")
password.send_keys("Your Facebook Password")
# Locating Submit Button
submit_button = driver.find_element_by_xpath("//input[@type ='submit']")
submit_button.click() # Button Click
# End
Output:- 
11. Complete Code to Demonstrate Login Into Facebook
Python3
# Python program to demonstrate Facebook Login
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://mbasic.facebook.com")
# Creating a Reference of Form For Finding Email and Password
form = driver.find_element_by_xpath("//form[@id ='login_form']")
email = form.find_element_by_name("email")
password = form.find_element_by_name("pass")
email.send_keys("singh.swaraj1999@gmail.com")
password.send_keys("Your Facebook Password")
submit_button = driver.find_element_by_xpath("//input[@type ='submit']")
submit_button.click()
# Error Password in Output
# Because i had not used my real password here
# End
Output:- 
Similar Reads
Exceptions - Selenium Python
Exceptions in Selenium Python are the errors that occur when one of method fails or an unexpected event occurs. All instances in Python must be instances of a class that derives from BaseException. Two exception classes that are not related via subclassing are never equivalent, even if they have the
3 min read
Action Chains in Selenium Python
Seleniumâs Python Module is built to perform automated testing with Python. ActionChains are a way to automate low-level interactions such as mouse movements, mouse button actions, keypress, and context menu interactions. This is useful for doing more complex actions like hovering over and drag and
4 min read
Python 3 basics
Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.11.0, we can simply call it Python3. Python 3.0 was released in 2008. and is interpreted language i.e it's not compiled and the interpreter will check the code line by line. This article can be used to learn the
10 min read
back driver method - Selenium Python
Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout â Navigating links using get method â Selenium Python. Just bein
2 min read
Selenium Python Tricks
Selenium: Selenium Python bindings provide a convenient API to access Selenium Web Driver like Firefox, Chrome, etc. What is webdriver? Selenium WebDriver is an automation testing tool. When I say automation, it means it automates test scripts written in Selenium. Webdriver Install Chrome: https://
3 min read
Special Keys in Selenium Python
Seleniumâs Python Module is built to perform automated testing with Python. Special Keys is an exclusive feature of Selenium in python, that allows pressing keys through keyboard such as ctrl+f, or shift+c+v, etc. class selenium.webdriver.common.keys.Keys handles all Keys in Selenium Python. It cont
1 min read
Selenium Python Tutorial
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python , Java , C# , etc, we will be working with Python. Selenium Tutorial cover
9 min read
Explicit waits in Selenium Python
Selenium Python is one of the great tools for testing automation. These days most web apps are using AJAX techniques. When the browser loads a page, the elements within that page may load at different time intervals. Table of Content What is Explicit Waits? How to create an Explicit wait in Selenium
4 min read
Implicit Waits in Selenium Python
Selenium Python is one of the great tools for testing automation. These days most of the web apps are using AJAX techniques. When a page is loaded by the browser, the elements within that page may load at different time intervals. This makes locating elements difficult: if an element is not yet pres
2 min read
Element methods in Selenium Python
Seleniumâs Python Module is built to perform automated testing with Python. Selenium in Python works with elements. An element can be a tag, property, or anything, it is an instance of class selenium.webdriver.remote.webelement.WebElement. After you find an element on screen using selenium, you migh
3 min read