Learning to use Python and Selenium by automating a google search.
Python is simple to use and mature. Someone can complete tasks with less number of lines. Python has a huge number of libraries and has good community support. Automation could be done by any programming language. Some of the advantages of Python are being open source, elegant syntax and ease of use.
Google search can be automated using Python script in just 2 minutes. This can be done using
selenium (a browser automation tool). Selenium is a portable framework for testing web applications. It can automatically perform the same interactions that any you need to perform manually and this is a small example of it. The limits of automation is endless with selenium.
Why is Automation important?
Automation does three main things, replaces human tedious tasks, minimizes error, and uses case helps. Businesses on their path to digital transformation. Organizations today are dealing with major disruption — think Airbnb, Amazon, etc. They’re challenged with supporting their employees and partners, reaching new customers, and providing new, innovative products and services faster.
For this project i’ll be using Sublime editor and MacOS.
- I guess it would be good idea to have Python, right? https://www.python.org/downloads/
- Selenium, you can pip install from your terminal.
pip install selenium
3. Chromedriver: Download the chrome browser from here (choose the version for your system). After downloading, extract it and then copy the file in the folder of the script.
The Automation Script
Copy and Paste the code below into your editor, then break it down for you.
Understanding The Python Script:
- from selenium import webdriver means that you want to import this certain function from a module into your work-space.
- The selenium.webdriver module provides all the WebDriver implementations. The Keys class provides keys in the keyboard like RETURN, F1, ALT etc.
- driver = webdriver.Chrome() creates the instance of Chrome WebDriver.
- The driver.get (URL) method will navigate to a page given by the URL. WebDriver will wait until the page has fully loaded before returning control to your test or script.
- WebDriver offers a number of ways to find elements using one of the find_element_by_* methods. For example, the input text element can be located by its name attribute using find_element_by_name method.
- A CSS Selector is a combination of an element selector and a value which identifies the web element within a web page.
- Google’s homepage currently has a large text input, with a name of “q.” The simplest xpath for this is a match: //input[@name=”q”].
- ******The last three points mentioned all work together at inputElems = driver.find_elements_by_css_selector(‘input[name=q]’)******
- Working with lists is described as using the keyword
forin combination with an iterator. For example: for inputElem in inputElems
- Finally, the send_keys method is used to send text to any field, such as input field of a form or even to anchor tag paragraph, etc. It replaces its contents on the webpage in your browser.
- For example: inputElem.send_keys(Keys.ENTER) presses the enter key like when you press enter in the search bar of google and then we will search “search string” by using inputElem.send_keys(‘Serch String…’)
Run The Automation:
Run the code with the following command in your terminal.
The results should pull up the following google search automatically.
To wrap this up, Selenium Automation has been considered by enterprises for multiple benefits, and most importantly to meet the objectives of Test Automation. The framework helps make the software testing process robust and cost-effective. However, this is only one very small example of automation with Python and Selenium. The possibilities are endless on what can be automated.