Welcome everyone to the new tutorial series about Selenium with python. This is the first basic introduction video tutorial.
Web UI Automation means the automatic execution of the actions performed in a web browser window like navigating to a website, filling forms that include dealing with text boxes, buttons, and dropdowns, submitting the forms, browsing through web pages, handling pop-ups, and so on. Selenium WebDriver is the one that can automate all these tasks. It can interact with all types of Web browsers available today, like Firefox, Internet Explorer, Safari, Chrome, Opera, etc.
Selenium is open-source, and its library is available in different programming languages to perform the Web UI Automation testing, and Python is one of them.
Selenium WebDriver Client Library for Python enables us to utilize all the features available with Selenium WebDriver and interact with Selenium Standalone Server to perform Automated testing (both remote and distributed testing) of browser-based applications.
I should mention that the Selenium Webdriver library is compatible with a series of Python versions that includes Python 2.6, 2.7, and 3.2-3.7. Almost all Python versions.
So I should mention that installing Selenium Webdriver on Python is as simple as doing that with other libraries. Simply use the following command:
pip install selenium
After installing Selenium, you should decide which browser you will use for your script. When I create something with Selenium, I am using Firefox, but everything should work the same way on Chrome, so it's up to you what you'll use. When you decided what browser you will use, you should download the selenium driver for your browser. Open THIS link, find GeckoDriver for Firefox or Chrome driver for Google Chrome, or choose whatever you are using. If you installed the browser to the default location, these drivers should be placed in the same folder where you are writing your selenium script, another way, you should add them to your environment variables. I am not sure how it works on Linux or iOS, but it shouldn't be hard to find it.
So bellow is my basic example script where we open a browser, going to the "www.google.com" website, and searching for Pylessons.com, next we are taking 10 of found elements and printing them out. Finally, we are closing our browser. A detailed code explanation is the below example.
from selenium import webdriver
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
# Navigate to the application home page
driver.get("http://www.google.com")
# get the search textbox
search_field = driver.find_element_by_name('q')
# enter search keyword and submit
search_field.send_keys("Pylessons.com")
search_field.submit()
# get the list of elements which are displayed after the search
# currently on result page using find_elements_by_class_name method
lists = driver.find_elements_by_class_name("LC20lb")
# get the number of elements found
print("Found "+str(len(lists))+" searches:")
# print not more than 10 list elements
i = 0
for listiems in lists:
print(listiems.text)
i += 1
if(i > 10):
break
# end session and close the browser window
driver.quit()
Decoding The Above Script
Let’s discuss the script line by line to better understand the Selenium WebDriver statements in brief. There will be a lot more about different Selenium WebDriver functions in upcoming tutorials.
Step 1.
The selenium webdriver module implements the classes that support different browsers, including Firefox, Chrome, Internet Explorer, Safari, others, and RemoteWebDriver to test on browsers available on remote machines. So We need to import webdriver from the Selenium package to use the Selenium WebDriver methods as:
from selenium import webdriver
Step 2.
Next, we need the browser's object, which we’ll use to load the web pages. The browser object provides a programmable interface to communicate with the browser using the Selenium commands. In the test script, we are using Firefox. We can create an instance of Firefox as shown in the following code:
driver = webdriver.Firefox()
On executing the above statement, a new Firefox window will launch. We are making the following settings for the driver instance:
driver.implicitly_wait(30)
driver.maximize_window()
We are configuring a timeout for Selenium to launch a browser or open a new tab in 30 seconds. The next statement maximizes the browser to full window.
Step 3.
Next, I am navigating to "http://www.google.com," passing the given URL to the driver.get() method. After making a call to the get() method, Webdriver waits until the page gets rendered in the browser window and sends the control back to the script.
After the page gets loaded, Selenium will interact with various elements on the page. Next, in the test script, we will be looking at different Selenium WebDriver functions that search an HTML object, send a text to the web component, simulate keypress events, click buttons and select from dropdowns, etc. Let’s see all these functions getting used in other steps.
Step 4.
First of all, we’ll locate the Google Search textbox to supply the text input for the Search. The Search text box has a name attribute as "q", and you can identify it from the code given below:
search_field = driver.find_element_by_name('q')
Step 5.
After locating the Search text box, we try to interact with the textbox element by using the send_keys() method to provide a new value. Subsequently calling the submit() method will forward the search request for processing.
search_field.send_keys("Pylessons.com")
search_field.submit()
After submitting the search request, the Firefox driver will display the result page returned by Google. The result page shows a list of entries that match the searched text. Each entry div element has a class of "LC20lb" and can be accessed using the "find_elements_by_class_name" method. This way, we'll get a list of elements.
lists = driver.find_elements_by_class_name("LC20lb")
The list of items may expand to too many pages, so we are restricting our code to print the first ten entries captured:
i = 0
for listiems in lists:
print(listiems.text)
i += 1
if(i > 10):
break
Summary
The above example gave us a real insight into using Selenium WebDriver and Python together to create a simple test automation script. It is a fundamental example script. We will use other interesting and complicated features of the Selenium Library with Python in upcoming tutorials.
However, if you like to go to the next level and wish to create a test automation suite in Selenium Python, follow future tutorials. There is only one way to learn Selenium, by automating stuff!