Selenium Webdriver Tutorial Python
Selenium tutorial provides basic and advanced concepts of Selenium. Our Selenium tutorial is designed for beginners and professionals.
Quick Wrapup – Selenium Webdriver Waits in Python. Understanding of Selenium Webdriver waits is a key to produce high-quality automation test scripts. We hope this tutorial would have directed you to the right approach. For more updates on Selenium Python tutorials, do follow our social media (Facebook/Twitter) accounts. Best, TechBeamers. Selenium Webdriver using Python: Tutorial with Example Selenium supports Python and thus can be utilized with Selenium for testing. Python is easy compared to other programming languages, having far less verbose. The Python APIs empower you to connect with the browser through Selenium. Run JavaScript In Selenium WebDriver. Below code will invoke javascript in selenium webdriver to scroll the scroll bar. This kind of action do not supported by selenium webdriver by default, so we need to use javascript to implement it. Import webdriver class from selenium import webdriver # create a google chrome web browser.
Selenium is one of the most widely used open source Web UI (User Interface) automation testing suite.
Our Selenium tutorial includes all topics of Selenium such as Features, Selenium vs QTP, Selenium Tool Suits, Selenium IDE, Selenium IDE Locating Strategies, Selenium WebDriver, WebDriver Features, WebDriver vs RC, WebDriver Installation, etc.
What is Selenium
Selenium is one of the most widely used open source Web UI (User Interface) automation testing suite.It was originally developed by Jason Huggins in 2004 as an internal tool at Thought Works. Selenium supports automation across different browsers, platforms and programming languages.
Selenium can be easily deployed on platforms such as Windows, Linux, Solaris and Macintosh. Moreover, it supports OS (Operating System) for mobile applications like iOS, windows mobile and android.
Selenium supports a variety of programming languages through the use of drivers specific to each language.Languages supported by Selenium include C#, Java, Perl, PHP, Python and Ruby.Currently, Selenium Web driver is most popular with Java and C#. Selenium test scripts can be coded in any of the supported programming languages and can be run directly in most modern web browsers. Browsers supported by Selenium include Internet Explorer, Mozilla Firefox, Google Chrome and Safari.
Selenium can be used to automate functional tests and can be integrated with automation test tools such as Maven, Jenkins, & Docker to achieve continuous testing. It can also be integrated with tools such as TestNG, & JUnit for managing test cases and generating reports.
Selenium Index
Selenium
Selenium IDE
- IDE- Locating Strategies
- IDE- Locating by CSS
Selenium WebDriver Tutorial
- Locating Strategies
- Locating Strategies By CSS
- Locating Strategies By XPath
TestNG with Selenium
Prerequisites
Before learning the concepts of Selenium, you should have a basic understanding of java or any other object-oriented programming language.
Languages supported by Selenium include C#, Java, Perl, PHP, Python and Ruby.Currently, Selenium Webdriver is most popular with Java and C#. So, if you know any of the languages then it won't be tough to understand the concepts of Selenium. In addition, you should have prior knowledge of software testing techniques like automation testing, functional testing, etc.
Audience
Our Selenium Tutorial is designed for all beginners and advanced-level Selenium users.
Selenium supports Python and thus can be utilized with Selenium for testing.
- Python is easy compared to other programming languages, having far less verbose.
- The Python APIs empower you to connect with the browser through Selenium.
- Selenium sends the standard Python commands to different browsers, despite variation in their browser's design.
You can run Python scripts for Firefox, Chrome, IE, etc.ondifferent Operating Systems.
In this tutorial, you will learn-
What is Python?
Python is a high-level object-oriented scripting language. It is designed in a user-friendly manner. Python uses simple English keywords, which is easy to interpret. It has less syntax complications than any other programming languages.
See some of the examples in the table below.Keyword | Meaning | Usage |
elif | Else if | Else if |
else | Else | if: X; elif: Y; else: J |
except | do this ,If an exception happens, | except ValueError, a: print a |
exec | Run string as Python | exec 'print 'hello world !' |
What is Selenium?
Selenium is a tool to test your web application. You can do this in various ways, for instance
- Permit it to tap on buttons
- Enter content in structures
- Skim your site to check whether everything is 'OK' and so on.
Why to choose Python over Java in Selenium
Few points that favor Python over Java to use with Selenium is,
1. Java programs tend to run slower compared to Python programs.
2. Java uses traditional braces to start and ends blocks, while Python uses indentation.
3. Java employs static typing, while Python is dynamically typed.
4. Python is simpler and more compact compared to Java.
How to Install and Configure PyDev in Eclipse
PyDev is Python development environment for Eclipse.
Step 1) Got to Eclipse Marketplace. Help > Eclipse Marketplace
Now once the plugin 'eclipse market place' is opened. The next step is to install 'pydev IDE' for eclipse.
Step 2) In this step,
Get drivers and downloads for your Dell Dell Keyboard - Smartcard USB. Download and install the latest drivers, firmware and software. Support Dell.com DellEMC.com Search. Dell keyboard driver windows 10.
- Search for 'pydev' in search box and then
- Click install(In my system it is already installed).
Step 3) Select the checkbox button. It says 'PyDev.' The first check box is mandatory while the second one is optional. After marking the checkbox, press 'Next'.
Step 4) Now, in this step you will set preferences. With the help of preference option, you can use Python as per the project need.
Go to Windows > Preferences > Interpreter-Python. Click on 'OK' button.
A new window will open when you click on 'OK' button. In this window, follow the following steps.
- Under interpreter dropdown, you select the option Interpreter-Python. It helps in running Python scripts.
- Also, set workbench time interval. When a build is performed, the workbench will automatically save all resources that is changed since the last build.
- Click on 'OK' button.
When you click on'OK' button, it sets the default Python Interpreter. It is just like you need to set java compiler for running a Java code. To change the interpreter name, double click on Python Tab.
Step 5)In this step, give the 'interpreter name' and the 'exe file name' of Python.
- Click on 'Browse' and find python.exe 'C:Python27python.exe.
- Click 'OK' button.
Step 6) Make a New Project in Python. In this step,
- Right click Package Explorer > New >
- Select option others.
You can see the new Python(PyDev) project is created.
Step 7) In this step,
- Select 'PyDev Project' and
- Press 'Next' button.
After creating 'PyDev Project', you will create a new Python package.
Step 8) Create a new Python package. After entering the name, click on 'Finish' button.
If you see in below screenshot, a new package is created.
After creating a new package, the next step is to createPyDev Module. The module contains somePython files for initialization. These files or functions from the module can be imported into other module. So, there will be no need to re-write the program again.
Step 9) Createa new PyDev module. Right click on package > New >Other>PyDev module.
Step 10) Write your Python code.
How to Create Test Scripts in Selenium with Python
In this example, we did automation for 'Facebook login page' using the Firefox driver.
EXAMPLE 1
Snapshot of the Code
Explanation of the code
- Code line 1: From selenium module import webdriver
- Code line 2: From selenium module import Keys
- Code line 3: User is a blank variable which will be we used to store values of username.
- Code line 4: pwd is also a blank (here it is empty, but the user can provide values in it) variable. This will be used to store values of the password.
- Code line 5: In this line, we are initializing 'FireFox' by making an object of it.
- Code line 6: The 'driver.get method' will explore to a page given by the URL.WebDriver will hold up until the page has completely been loaded (that is, the 'onload' occasion has let go), before returning control to your test or script.
- Code line 7: 'Asserts' keyword is used to verify the conditions. In this line, we are confirming whether the title is correct or not. For that, we will compare the title with the string which is given.
- Code line 8: In this line, we are finding the element of the textbox where the 'email' has to be written.
- Code line 9: Now we are sending the values to the email section
- Code line 10: Same for the password
- Code line 11: Sending values to the password section
- Code line 12: Elem.send_keys(Keys.RETURN) is used to press enter after the values are inserted
- Code line 13: Close
OUTPUT
The values of the username 'guru99' and password entered.
The Facebook page will login with email and password. Page opened (see image below)
EXAMPLE 2
In this example,
- We will open a login page.
- Fill the required field'username' and 'password'.
- Then validate if the login was successful or not.
Snapshot of the code
Explanation of the code:
- Code line 1-2: Import selenium package
- Code line 4: Initialize Firefox by creating an object
- Code line 5: Get login page (Facebook)
- Code line 7-9: Fetch username, password input boxes and submit button.
- Code line 11-12: Input text in username and password input boxes
- Code line 15: Click on the 'Submit' button
- Code line 18: Create wait object with a timeout of 5 sec.
- Code line 20 -30: Test that login was successful by checking if the URL in the browser changed. Assert that the URL is now the correct post-login page
Selenium Webdriver Tutorial Python Video
Summary:
Selenium Webdriver Tutorial Python
- Selenium is an open-source web-based automation tool.
- Python language is used with Selenium for testing. It has far less verbose and easy to use than any other programming language
- The Python APIs empower you to connect with the browser through Selenium
- Selenium can send the standard Python commands to different browsers, despite variation in their browser's design.