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
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https: / / mbasic.facebook.com")
|
How to Use Other Webdriver
Python3
driver = webdriver.Firefox()
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
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https: / / mbasic.facebook.com")
html = driver.page_source
print (html)
|
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
driver.find_element_by_id("m_login_email")
|
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
driver.find_element_by_name(" pass ")
|
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
driver.find_element_by_link_text("Forgotten password?")
|
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
driver.find_element_by_partial_link_text("Forgotten password?")
|
5. XPath
HTML Elements can be Easily Found by using this For Example :- Find Email and Password Input Field
Python3
form = driver.find_element_by_xpath(" / / form[@ id = 'login_form' ]")
email = form.find_element_by_name("email")
password = form.find_element_by_name(" pass ")
|
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
title = driver.find_element_by_tag_name("title")
print (title)
|
7. CSS Selector
HTML Elements can be Easily Found by using CSS For Example :- Finding Elements by using Class, style, etc.
Python3
password = driver.find_element_by_css_selector( 'input.bl.bm.bo.bp' )
|
8. Class Name
HTML Elements can be Easily Found by using Class name For Example :- Finding Elements by using Class Name.
Python3
password = driver.find_element_by_class_name( 'bl bm bo bp' )
|
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
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")
|
10. Locating Submit Button
It is Used to Locate the Submit Button Which is used to submit a form
Python3
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()
|
Output:-
11. Complete Code to Demonstrate Login Into Facebook
Python3
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https: / / mbasic.facebook.com")
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()
|
Output:-
Last Updated :
01 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...