Selenium Python Basics

27 Feb, 2020
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.
Webdriver
Getting Source of a Web-Page/URL
Locating Elements ByIdNameLink TextPartial Link TextXPathTag NameCSS SelectorClass Name
IdNameLink TextPartial Link TextXPathTag NameCSS SelectorClass Name
Id
Name
Link Text
Partial Link Text
XPath
Tag Name
CSS Selector
Class Name
Entering Input in an Input Field Using Selenium Script
Locating Submit Button
Complete Code to Demonstrate Login Into Facebook
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 form here for chromium browser
# Python program to demonstrate# Webdriver For Firefoxfrom selenium import webdriverdriver = webdriver.Firefox()driver.get("https://mbasic.facebook.com")
# Firefox driver = webdriver.Firefox()# Google Chrome driver = webdriver.Chrome()
By Using this one can get the complete page source/code of current opened URL/Web-Page.
# Python program Continued# Webdriver For Firefoxfrom selenium import webdriverdriver = webdriver.Chrome()driver.get("https://mbasic.facebook.com")html = driver.page_source # Getting Source of Current URL / Web-Page Loadedprint(html)# End
Output:
For Example :-
HTML Elements have some attribute “id” which can be used to Locate Those Elements.For Example :- Finding Input Box For Email Field
# Python program Continued  # Finding Input Box For Email Field# Go Through the Screen Shot Above or Page Sourcedriver.find_element_by_id("m_login_email") # End
HTML Elements Have attribute “name” associated with them which can be used to Locate Those Elements.For Example :- Finding Input Box For Password Field
# Python program Continued  # Finding Input Box For Password Field# Go Through the Screen Shot Above or Page Sourcedriver.find_element_by_name("pass")    # End
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
# Python program Continued  # Finding Forgotten Password Link Fielddriver.find_element_by_link_text("Forgotten password?")# End
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
# Python program Continued  # Finding Forgotten Password Link Fielddriver.find_element_by_partial_link_text("Forgotten password?")# End
HTML Elements can be Easily Found by using thisFor Example :- Find Email and Password Input Field
# Python program Continued  # Creating a Reference of Form For Finding Email and Password# Reference for Form Findingform = driver.find_element_by_xpath("//form[@id ='login_form']")# Emailemail = form.find_element_by_name("email") # Passwordpassword = form.find_element_by_name("pass") # End
HTML Elements can be Easily Found by using Tag NameFor Example :- Finding Elements by using tag name like Title, Head, Body, Html, a, div, etc.
# 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
HTML Elements can be Easily Found by using CSSFor Example :- Finding Elements by using Class, style, etc.
# 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
HTML Elements can be Easily Found by using Class nameFor Example :- Finding Elements by using Class Name.
# 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
It is Used to Insert Input into Input Field Using inbuilt Function send_keys.
# Python program Continued   # Creating a Reference of Form For Finding Email and Password  # Reference for Form Findingform = 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 Fieldemail.send_keys("singh.swaraj1999@gmail.com")   # Inserting("Your Facebook Password") in the Password Input Field password.send_keys("Your Facebook Password")     # End
It is Used to Locate the Submit Button Which is used to submit a form
# Python program Continued   # Creating a Reference of Form For Finding Email and Passwordform = 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 Buttonsubmit_button = driver.find_element_by_xpath("//input[@type ='submit']") submit_button.click()  # Button Click# End
Output:-
# Python program to demonstrate Facebook Loginfrom selenium import webdriver  driver = webdriver.Firefox()  driver.get("https://mbasic.facebook.com")  # Creating a Reference of Form For Finding Email and Passwordform = 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:-
Articles
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Time Complexity and Space Complexity
SQL | Views
Understanding "extern" keyword in C
SQL Interview Questions
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
Python Dictionary
How to get column names in Pandas dataframe