Selenium webdriver python tutorial for web automation

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:-

  1. Enter the content in the structures.
  2. Permitting it to tap on buttons.
  3. Skim the site to check if everything is OK.

Why we choose python language instead of java in selenium:-

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.

How selenium is used in selenium for testing:-

  1. It is an easy language compared to other languages.
  2. Python API will empower you to connect with the browser through the selenium tool.
  3. It will send standard commands to different browsers and variations.

Browsers supported by selenium web drivers are:-

  1. Internet explorer driver
  2. Google chrome driver
  3. Opera driver
  4. Safari driver
  5. HTML unit driver

Selenium web driver:-

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.

The selenium webdriver architecture:-

AAPI will provide a communication facility between languages and browsers.

  1. Real browsers
  2. Selenium client libraries
  3. Browser driver
  4. JSON wire protocol.

1. JSON protocol:-

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.

2. Browser driver:-

While executing test script webdriver for following operations,

  1. The HTTP request is generated for each selenium command.
  2. The driver receives the HTTP through the HTTP server.
  3. The driver will decide all steps to perform which is executed on a browser.

3. Browsers:-

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.

4. Selenium client libraries:-

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.

Selenium webdriver features:-

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)

install selenium webdriver python tutorial for web automation

  1. The process meets four steps to complete,
  2. Download and install a higher version if available.

Common selenium commands in the webdriver:-

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”);

11. Submit ():-

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.

Installation of webdriver ():-

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’’)

Install and config pyDev in eclipse:-

It is the python development enviourement for eclipse (PyDev)

  1. Go to eclipse.
  2. Help>install new software

selenium webdriver python tutorial

  1. Then install “pydev IDE” foe eclipse
  2. Search http://pydev.org/updates in work with select items and click next. Also accept license agreement and click finish.

selenium webdriver with python video tutorial

  1. Then click on anyway. In this set preferences and use python as need.

python selenium tutorial

  1. Then go to windows>preferences>pyDev>interpreter>python Interpreter.

selenium webdriver python tutorial for web automation

  1. Then set the default Python Interpreter. Same as you set java compiler for running a Java code.
  2. If you want to change the interpreter name, click on Browse for python/pypy exe Button.

pycharm selenium tutorial

  1. Then click on 'Browse', find python.exe where you installed python.
  2. Also, click the 'OK' button.
  3. Then Select all the Folder and click on OK
  4. Last Click on "Apply and Close".

selenium tutorial python chrome

  1. Create New Project in Python then,
  2. Then Right click PyDev Package Explorer > New.
  3. And Select option others.

selenium tutorial using python

  1. Select "PyDev > PyDev Project".
  2. And Press 'Next' button.

python selenium tutorial chrome

  1. Lastly Name your Project
  2. And Click "Finish".

python selenium tutorial chrome

  1. See the new Python (PyDev) project is created.
  2. After creating the 'PyDev Project', you will create a new Python package.
  3. Then right-click on Project > New > PyDev Package.
  4. Also, name your Package and Click Finish.

python selenium tutorial chrome

python selenium tutorial chrome

 

  1. See the screenshot, a new package is created.

python selenium tutorial chrome

  1. After the creation of a new package, the next step is to createPyDev Module.
  2. The module contains somePython files for initialization.
  3. The functions from the module can be imported into other module.
  4. We have to create a new PyDev module,
  5. Right-click on package > New > PyDev module.

python webdriver tutorial


selenium web scraping python tutorial

  1. Selection of Empty Template and Click "OK".

python selenium web scraping tutorial

  1. Then write your Python code.

Selenium webdriver python tutorial for web automation Example:-

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:-

  1. First step is to selenium module import webdriver
  2. From selenium module import Keys
  3. User is a variable that will be used to store values of username.
  4. Variable "password" is used to store values of the password.
  5. We initialize the "Firefox" by making the object of it.
  6. WebDriver will wait until the page is completely loaded before returning control to your test or script.
  7. Then send the values to the email section
  8.  Then Same for the password
  9. Send values to the password section
  10.  element.send_keys (Keys. RETURN) is used to enter after the values are inserted
  11.  Close

Output:-
           Enter the username "guru99" and password entered.

selenium webdriver python tutorial for web automation


selenium webdriver python tutorial

Advantages of Selenium WebDriver:-

  1. It is the simple Installation process
  2. Also more realistic browser interaction.
  3. It is a separate component such as a RC server.
  4. It has a faster Execution time
  5. Acts as an Open Source.
  6. Has the capability to run tests across different browsers
  7. Support multiple operation systems
  8. It Support mobile devices
  9. Capability to execute the tests in parallel

Disadvantages of Selenium WebDriver:-

    • It has no ability to access elements external to the web application
    • There is lack of official user support for Selenium
    • lack of native support to generate test reports
Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India