Original URL: https://www.guru99.com/selenium-python.html
How to use Selenium with Python:complete Tutorial
Selenium supports Python and thus can be utilized with Selenium for testing.
- Python is easy compared-to-other programming languages, has 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 the Python scripts for Firefox, Chrome, IE, Etc.ondifferent Operating Systems.
In this tutorial, you'll learn-
- What is Python?
- What is Selenium?
- Why to choose Python over Java in Selenium
- Install and Configure PyDev in Eclipse
- Create Test Scripts in Selenium with Python
What is Python?
Python is a high-level object-oriented scripting language. It's designed in a user-friendly manner. Python uses simple 中文版 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. Various ways, for instance
- Permit it to tap on buttons
- Enter content in structures
- Skim your site to check whether everything are "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.
Install and Configure PyDev in Eclipse
PyDev is a 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 the "Pydev IDE" for Eclipse.
Step 2) In this step,
- Search for "Pydev" in search box and then
- Click install (in the My system it is already installed).
Step 3) Select the checkbox button. It says ' PyDev. ' The first check box is mandatory and the second one is optional. After marking the checkbox, press ' Next '.
Step 4) Now, in This step you'll 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 would open when you click on the ' 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 would automatically save all resources, which 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 and double click on the 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:\Python27\python.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'll 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 was to Createpydev Module. The module contains somepython files for initialization. These files or functions from the module can is imported into other module. So, there'll be is no need to re-write the program again.
Step 9) Createa new PyDev module. Right click on the package > New >other>pydev module.
Step Ten) Write your Python code.
Create Test Scripts in Selenium with Python
- In this example, we do automation for "Facebook login page" using the Firefox driver.
EXAMPLE 1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
user = ""
pwd = ""
driver = webdriver.Firefox()
driver.get("http://www.facebook.com")
assert "Facebook" in driver.title
elem = driver.find_element_by_id("email")
elem.send_keys(user)
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)
driver.close()
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 would be we used to store values of username.
-
- Code Line 4: PWD was also a blank (here it was empty, but the user can provide values in it) variable. This is used to store values of the password.
-
- Code Line 5: On this line, we is initializing "FireFox" by making an object of it.
-
- Code Line 6: The "Driver.get method" would explore to a page given by the URL. Webdriver'll hold up until the page have completely been loaded (that's, the "onload" occasion have let go), before retur Ning control to your test or script.
-
- Code Line 7: "Asserts" keyword are used to verify the conditions. In this line, we are confirming whether the title is correct or not. For that, we'll compare the title with the string which is given.
-
- Code Line 8: On this line, we is finding the element of the textbox where the "email" has been written.
-
- Code Line 9: Now we is sending the values to the e-mail section
-
- CodeLine: Same for the password
-
- CodeLine one: Sending values to the password section
-
- CodeLine: Elem.send_keys (Keys.return) was used to press ENTER after the values are inserted
-
- CodeLine: Close
OUTPUT
The values of the username "guru99" and password entered.
The Facebook page would login with email and password. Page opened (see image below)
EXAMPLE 2
In this example,
- We'll open a login page.
- Fill the required field "username" and "password".
- Then validate if the login is successful or not.
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox()
browser.get( www.facebook.com )
username = browser.find_element_by_id( "guru99" )
password = browser.find_element_by_id( "[email protected]" )
submit = browser.find_element_by_id( "submit" )
username.send_keys( "me" )
password.send_keys( "mykewlpass" )
submit.click()
wait = WebDriverWait( browser, 5 )
try:
page_loaded = wait.until_not(
lambda browser: browser.current_url == login_page
)
except TimeoutException:
self.fail( "Loading timeout expired" )
self.assertEqual(
browser.current_url,
correct_page,
msg = "Successful Login"
)
Snapshot of the Code
Explanation of the code:
Summary:
- 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.
[Selenium+java] How to use Selenium with Python:complete Tutorial