Selenium
How to Handle AJAX Call in Selenium Webdriver
What is Ajax? AJAX stands for Asynchronous JavaScript & XML, and it allows the Web page to...
Selenium supports Python and thus can be utilized as Selenium WebDriver with Python for testing.
You can run Selenium with Python scripts for Firefox, Chrome, IE, etc. on different Operating Systems.
In this Selenium Python tutorial, you will learn-
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 !"' |
Selenium is a tool to test your web application. You can do this in various ways, for instance
Few points that favor Python over Java to use with Selenium is,
PyDev is Python development environment for Eclipse.
Step 1) Got to Eclipse Marketplace. Help > Install New Software
The next step is to install "pydev IDE" for eclipse.
Step 2) In this step,
Step 3) You may encounter Security Warning, Click on "Install Anyway".
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 Window > Preferences > PyDev > Interpreter > Python Interpreter.
Let's 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, click on Browse for python/pypy exe Button.
Step 5) In this step, give the "interpreter name" and the "exe" file path of Python.
Step 6) Make a New Project in Python. In this step,
You can see the new Python (PyDev) project is created.
Step 7) In this step,
After creating 'PyDev Project', you will create a new Python package.
Step 8) 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 some Python 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 > PyDev module.
Name your Module and click "Finish".
Select Empty Template and Click "OK".
Step 10) Write your code for Selenium with Python as shown below
In this Selenium WebDriver with Python example, we did automation for "Facebook login page" using the Firefox driver.
Selenium Python Example 1: 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_element_by_id("email")
element.send_keys(user_name)
element = driver.find_element_by_id("pass")
element.send_keys(password)
element.send_keys(Keys.RETURN)
element.close()
Snapshot of the Code
Explanation of the code
OUTPUT
The values of the username "gtupapers" and password entered.
The Facebook page will login with email and password. Page opened (see image below)
EXAMPLE 2: Login into Facebook & Check Title
In this example,
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
# Step 1) Open Firefox
browser = webdriver.Firefox()
# Step 2) Navigate to Facebook
browser.get("http://www.facebook.com")
# Step 3) Search & Enter the Email or Phone field & Enter Password
username = browser.find_element_by_id("email")
password = browser.find_element_by_id("pass")
submit = browser.find_element_by_id("loginbutton")
username.send_keys("YOUR EMAILID")
password.send_keys("YOUR PASSWORD")
# Step 4) Click Login
submit.click()
wait = WebDriverWait( browser, 5 )
page_title = browser.title
assert page_title == "Facebook"
Snapshot of the code
Explanation of the code:
Summary:
What is Ajax? AJAX stands for Asynchronous JavaScript & XML, and it allows the Web page to...
In this tutorial, we will learn handling Keyboard and Mouse Event in Selenium Webdriver Action...
To understand extensions, lets first understand the three pillars of selenium IDE Action: What...
Accessing Image Links Image links are the links in web pages represented by an image which when...
What is Apache Ant? While creating a complete software product, one needs to take care different third...
There are two types of HTML tables published on the web- Static tables : Data is static i.e....