Selenium webdriver python tutorial for web automation with example: In this tutorial, we are going to discuss the selenium web driver using python language. Before learning the web driver we should have knowledge of any programming language.
It is introduced as v2.0 and v1 consist of RC, Grid, and IDE.
The test scripts are developed using the programming language and run in web browsers.
There are many languages that support webdriver as PHP, python, java.
It will perform fast as compared to RC and call to web browsers.
Also, build on the implementation of Firefox driver and need to plug-in for drivers and run the test. The selenium is a tool used to test the web applications and the ways to follow the test are as follows:-
The program in java runs slowly as compared to python.
It uses traditional braces to start and end block while python uses indentation.
Python is a simple language and java is a complicated language.
It is a very important selenium tool and the latest version is selenium 2.0 and integrated with web driver.
The selenium webdriver is called as the popular tool for Web UI automation.
The web UI automation means the automatic execution of the actions performed in the windows like navigating the website.
It will include the radio button, text boxes, submitting forms through the web pages.
The selenium web driver will automate tasks and interact with web browsers like Firefox, safari, and chrome.
The java and eclipse are used to create the selenium web driver project.
It utilizes all features available with the selenium tool.
AAPI will provide a communication facility between languages and browsers.
The long-form is JavaScript Object Notation and is an open standard for exchanging the data on the web.
It is a wire protocol that provides a mechanism to transfer the data between a server and client.
It serves as the industry standard for various REST web services.
While executing test script webdriver for following operations,
These drivers are available for browsers like internet explorer, safari, etc.
Also you can use them for performing browser testing.
You cannot perform the browser testing of websites whose driver is not available publicly.
The developers use selenium to write test code in languages like c#, python, Perl, etc.
The language will support possible due to the client libraries.
The features of the webdrivers are as follows,
Multiple language support:-
The web driver will support the commonly used language like Ruby, pearl, C#.
Speed:- Will perform fast tool as compared to others.
Simple command:- The commands used in the web drivers are easy to implement and launch in web driver.
Commands:-
WebDriver=new Chrome Driver () ;( chrome browser)
WebDriver=new Firefox Driver () ;( Firefox browser)
WebDriver driver=new InternetExplorerDriver () ;( Internet Explorer browser)
1. Fetching webpage:-
Two methods for fetching webpage’s
Using Get method as,
Driver. get (“www.javatpoint.com”)
Using the navigate method as,
driver. navigate ().to (“https://javatpoint.com/selenium-tutorial”);
driver. navigate ().to (“https://javatpoint.com/selenium-tutorial”);
2. Clearing user input:-
clear() method is used to clear the user input from text box.
3. Locate form and send user input-
Driver.findElement (By.id (“list-lib”)).send Keys (“java point tutorials”);
4. Performing click:-
Used to click () on web element.
5. Fetch data over web element:-
We use the gettext () method to fetch the data written over any web element.
6. Navigate backward browser history:-
Driver. navigate (). Back ();
7. Navigate forward browser history:-
Driver. navigate (). ford ();
8. refresh/reload a web page:-
Driver.navigate ().refresh ();
9. Closing browser:-
Driver. close ()
10. Moving between windows:-
Driver.swithchTo ().window (“window name”);
Used to submit the web form. Here submit method is good for the click () if element is clicked.
driver.findElement (By.xpath ("//input [@name='comments']")).submit ();
12. Find element (By. by) with size ():-
It can be used to verify an element that is present on the webpage.
Boolean checkIfElementPresent=driver.findElement (By.xpath (“//input [@id=’checkbox2’]”)).size ()! =0;
13. Close () and quit ():-
These commands are used at the automation program.
Driver. close ():-It closes the current window.
Driver. quit ():-It quits the driver and close every window which is opened.
In this example, the automation for "Face book login page" using the Firefox driver.
To use the selenium webdriver we should install it as,
pip install selenium
Using the webdriver as,
From selenium import webdriver
driver=web driver. chrome ()
After the initialization of driver use instance by writing browser.
Navigating page with webdriver is simple:
Driver. get (‘http://example.com’’)
It is the python development enviourement for eclipse (PyDev)
Login into facebook
from selenium import webdriver
from selenium.webdriver.common. keys import Keys
user_name=”YOUR EMAILID”
password=”YOUR PASSWORD”
driver=webdriver.firefox ()
driver. get (“https://www.facebook.com”)
element=driver.find_elemet_by_id (“email”)
element.send_keys (user_name)
element=driver.find_elemet_by_id (“pass”)
element.send_keys (password)
element.send_keys (keys.RETURN)
element.close ()
Code explanation:-
Output:-
Enter the username "guru99" and password entered.