What Is Selenium?
Selenium is an open-source project that provides many tools and libraries. These tools and libraries are helpful in web browser automation. Selenium helps author functional tests across various web browsers. Selenium is a powerful tool that helps write a test case across various platforms using a single interface. Selenium provides the ability to write a robust test case in various programming languages such as Java, C#, Ruby, Php, Python, Perl, and others. Selenium helps in web browser automation by using a program that a developer can use on all the major operating systems.
Use Of Automation Testing
Primarily, projects now are using Agile methodology. This methodology encourages developers to make frequent changes in their programs. Since there are many significant changes, they require testing regularly. Manual testing only proves helpful in scenarios where there are multiple browsers. Therefore, Automation testing saves the hassle in such cases. Automation testing helps in adding various features of user engagement to a website. Moreover, Selenium is the best for automation testing, given its tools and libraries.
Python Automation Using Selenium
Selenium is an efficient tool that helps define test scripts and detects their results automatically. The developers can pre-decide a web browser to work for automation testing. The Selenium functions allow creating a step-by-step interaction with the desired webpage and analyzing the user interface’s response according to changes. Moreover, Python helps in automation as it is one of the recent technologies’ most powerful scripting languages.
Guide to Using Selenium with Python for Automation
Following is a guide to using Selenium and Python to write efficient scripts for automation testing. The developers can use Selenium to perform automated operations such as monitoring tweets, googling anything without opening the web browser, and automated testing on Whatsapp. Following are the steps to use while working with Automation testing in Selenium:
Selenium Installation
The bindings of Selenium and Python help provide some helpful APIs that can help write various functional tests. These functional tests require using Selenium WebDriver. Moreover, this API helps create efficient functional tests interacting with web elements on the Application Under Test (AUT). Therefore, these bindings support local web browsers, including Firefox, Internet Explorer, Puppeteer, Chrome, and many others. Furthermore, these bindings provide a remote connection to the cloud Selenium Grid using a remote WebDriver. The Python package management system, pip, helps install Selenium on the user’s device. The users can use the following simple command to install Selenium on a Python environment:
pip install selenium.
After acquiring the basic Selenium functionalities in Python by installing, the next step is to include various drivers and basic functionalities to interact with the desired web browser.
Web Drivers
As mentioned above, web drivers must be installed to interact with the required browser. A web driver is a package needed to interact with a web browser. The web drivers interact with the web browsers or remote web servers using a wire protocol. Some famous web drivers for automation testing are
Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads Firefox: https://github.com/mozilla/geckodriver/releases Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/ Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Moreover, if the users want to test Selenium locally, installing the packages and drivers is a sufficient approach. On the other hand, if the users want to set Selenium for a remote server, they need to install the Selenium Server written in Java.
Automation Example
For this example, the users can work with the Chrome web browser to test a simple example of automation. The first step in automation is to set up Selenium using Python. The users have to import the WebDriver and the “keys” class from Selenium using the following commands:
from selenium import webdriver from selenium.webdriver.common.keys import Keys
The class “webdriver” helps the users connect to a browser’s instance. The class “keys” enables the users to emulate the keyboard keystrokes. Moreover, these keystrokes include various special keys, such as shift and return. The Key class helps perform relevant keyboard operations on WebElements in the DOM.
Chrome Instance
The next step is the creation of an instance of the respective browser with which the user is working. In this example, the step is to create an instance of the chrome browser. This step is crucial for executing a test. The developers have to create an instance using the same path they used to download through the website. Assuming that the web driver is in the same directory as the Python program which will execute, the users can use the following command to create a chrome instance:
driver = webdriver.Chrome('./chromedriver')
If the users are testing on their local machines, the above command opens the chrome instance locally. The users can perform automation testing on this instance until the users want to end the connection to the browser by using the “close()” function.
Loading a Website
The next step is to load a website using the “get()” function. The users can also load a local development site, equivalent to opening a chrome window on their local machine. The advantage of using the get() function is to load a website and wait for it to render entirely before jumping onto the next step. Another option is to use the sleep() function. The sleep() function is used because it helps the user watch until the application is loaded completely, and Selenium does not work unless all the page elements are loaded. Following is the command to load a website using the get() function:
driver.get("https://www.python.org")
Using the get() function along with the sleep() looks like the following command:
driver.get("https://www.python.org") time.sleep(2)
Access the Title
After entirely loading a website, the next step is interacting with the title using the title attribute. The users can also check if the title contains a particular string. The users can utilize the assert or if statements to check this attribute. The following command prints the title of the page:
print(driver.title)
Query Submission
The next step is to submit a query in the search bar. First, the users must select the specific element from the HTML DOM to enter a value and submit the form using Return key emulation. The users can select the specific element using the CSS class, attribute, id, or name. Therefore, the users can use the find_element_by_name() function to select a specific element using the following command:
search_bar = driver.find_element_by_name("a")
Clearing the Content
The users can select the element to clear its contents using the clear() function. The following commands help in clearing the contents of an element:
search_bar.clear() search_bar.send_keys("getting started with python") search_bar.send_keys(Keys.RETURN)
Similarly, the users can perform other desired actions using Selenium automation abilities. Another example of using Selenium automation in Python is the following:
from selenium import webdriver import time from selenium.webdriver.common.keys import Keys browser = webdriver.Firefox() browser.get('https://www.twitter.com') time.sleep(2) login = browser.find_elements_by_xpath('//*[@id="doc"]/div[1]/div/div[1]/div[2]/a[3]') login[0].click() print("Login in Twitter") user = browser.find_elements_by_xpath('//*[@id="login-dialog-dialog"]/div[2]/div[2]/div[2]/form/div[1]/input') user[0].send_keys('USER-NAME') user = browser.find_element_by_xpath('//*[@id="login-dialog-dialog"]/div[2]/div[2]/div[2]/form/div[2]/input') with open('test.txt', 'r') as myfile: Password = myfile.read().replace('\n', '') user.send_keys(Password) LOG = browser.find_elements_by_xpath('//*[@id="login-dialog-dialog"]/div[2]/div[2]/div[2]/form/input[1]') LOG[0].click() print("Login Successful") time.sleep(5) elem = browser.find_element_by_name("q") elem.click() elem.clear() elem.send_keys("Geeks for geeks ") elem.send_keys(Keys.RETURN) print("Search Successful") browser.close()
This example logs in to Twitter and searches for the “geeks for geeks” handle.