Selenium With Python Tutorial

Orange TechnoMind 21-Mar-2019
orange technomind

Selenium with Python 

Selenium WebDriver is one of the most popular tools for web Automation. Selenium Python bindings gives a simple API to put in writing functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you may access all functionalities of Selenium WebDriver in an intuitive way.

Selenium Python bindings provide a convenient API to access Selenium WebDrivers like Firefox, IE, Chrome etc. The present day supported Python versions are 2.7, 3.5 and above

Why Python ?

Python is the popular Programming Language it has the simplicity of C-Programming-language and is an OBJECT-ORIENTED Programming Language like JAVA.

Google use python for its Search Engine.Instagram and Pinterest use Python's Django Framework.we can use pygame For developing games in Python.we can use modules unittest, pytest,nose For Testing.we can use django For web-development.For standalone Application we can use tkinter.For web-scrapping we can use urlib, BeautifulSoup

Installation

Pip is the package management tool used to install and manage software package, written in Python.Virtualenv provides an isolated working environment which allow us to work on a specific project without worrying about affecting other projects.

Steps to be followed before writing a Test Scrips

  • Understand the Requirement
  • Write Test Plan and Test Cases
  • Decide which Test Case to Automate
  • Inspect the required Web UI elements 
  • Starting writing your Test Script

Navigation In Selenium Python

The Selenium WebDriver provides “get()” method this method is used to open a web page.

driver.get("http://www.google.com")

WebDriver returns the manipulate to the test script after the web page gets completely loaded. but, if the net page makes use of a lot of AJAX, the WebDriver won't be able to decide, while it has loaded absolutely. For such pages, we can use WebDriver to ensure that the web page gets loaded completely.

Interacting With Textarea And Text Field

WebDriver calls for locating that element first earlier than it is able to perform any action on it.

Example

we will locate it using either of the subsequent techniques:

element = driver.find_element_by_id("user_name")

element = driver.find_element_by_name("user")

element = driver.find_element_by_xpath("//input[@id='user_name']"

After locating the element, we are able to enter some textual content in the text field as:

element.send_keys("message") : Whatever message we send through ‘send_keys’ gets appended to what’s already there.

element.send_keys(" and some", Keys.ARROW_DOWN) : Moving up and down using arrow keys in a multi-line text box

element.clear() : This methos is used to clear the contents of a text field or textarea

Interacting With Drop-Down

With Selenium Python, there come two ways to interact with the drop-down list.

1. Using Find Element API

2. Using Select API

1. Using Find Element API

using “find_element_by_xpath”, or “find_element_by_id” functions we can locate the drop-down element

using “find_elements_by_tag_name” function we can retrieve the list of options in the drop-down  

Example

element = driver.find_element_by_xpath("//select[@name=' Numbers ']")

all_options = element.find_elements_by_tag_name("option")

for option in all_options:

    print("Value is: %s" % option.get_attribute("value"))

    option.click()

2. Using Select API

The other way is the recommended one. Selenium Python API provides the Select class, which contains useful methods for handling these select web elements.

Using The Index Of Drop-Down : Selenium Webdriver provides the “select_by_index( index )” method to select an option using the index attribute.It throws “NoSuchElementException,” If there is no option with the specified index.

Example

element = Select(driver.find_element_by_id('id_of_element'))

element.select_by_index(1)

Using The Value Of Drop-Down : we use the “select_by_value(value )” method to select an item from the drop-down matching to the value.

Example

<option value="int">Number</option>

then the following code helps to select by value

element = Select(driver.find_element_by_id('id_of_element'))

element.select_by_value('int')

Using Text Of Dropdown : we use the “select_by_visible_text(text)” method select the item that matches with the text given in the method

For example,

element = Select(driver.find_element_by_id('id_of_element'))

element.select_by_visible_text('text_element')

De-Select Options From Dropdown

Deselect_all( ) :  It is to clear all the selected options.If we use this method in case of a single selection, it will throw a NotImplementedError exception.

Deselect_by_index( ) : using the “index” attribute this API clears the selected option . It is the reverse of the select_by_index( ) method.

Deselect_by_value( ) : This clears the selected option using the value of the option. It is the reverse of the select_by_value( ) method.

Deselect_by_visible_text( ) : This  clears the selected option using the text of the option. It is the reverse of the select_by_visible_text( ) method.

Switch between Windows

It is essential to understand how to use Selenium Python to switch between windows.using the “switch_to_window” method WebDriver supports moving between named windows.You can re-use this technique to solve real-time use cases in your projects.

Example : driver.switch_to_window("windowName")

Switch between Frames

You can also move from one frame to another frame using the method driver.switch_to_frame("frameName")

Popup Dialog Box

It  has built-in support for to handle popup dialog boxes.

alert = driver.switch_to_alert()

Locate Elements using Selenium Python

You can use the locators in order to perform any automated action on a web page

Id - Syntax: driver.findElement(By.id(“xxx”));

Name - Syntax: driver.findElement(By.name(“xxx”));

ClassName - Syntax: driver.findElement(By.className(“xxx”));

PartialLinkText - Syntax: driver.findElement(By.partialLinkText(“xxx”));

Linktext - Syntax: driver.findElement(By.linkText(“xxx”));

X-Path - Syntax: driver.findElement(By.xpath(“xxx”));

CSS - Syntax: driver.findElement(By.css(“xxx”));

TagName - Syntax: driver.findElement(By.tagName(“xxx”));

Selenium Webdriver Waits In Python

Selenium Webdriver provides two types of waits

1.Implicit

2.Explicit

Implicit wait

using Implicit wait in webdriver software program checking out test case, we are able to ballot the DOM for positive amount of time when some element or factors are not available straight away on website.

Explicit wait

The use of explicit wait code in selenium webdriver automation checking out device, you may define to watch for a sure situation to occur earlier than proceeding in addition check code execution.

 

Contact Us

If you have a question, we have an answer. If you need something, we'll help you. Contact us anytime.

For enrollment related queries & support

+91 99 40 550 285

For technical issues & support

orangetechnomind@gmail.com