Selenium is a library that enables the automation of web browsers. It is most popularly used for testing web applications. At the heart of Selenium lies the Selenium WebDriver. WebDriver uses browser automation APIs to control the browser and run tests. This would mean the browser would work in the same way as when it is used by a real user. You can use these features to automate most of your boring stuff online, for example, applying to multiple jobs on LinkedIn and AngelList.
This article is about using Selenium (Python) to automate a form-filling process, i.e. filling a form multiple times. After that, we step things up a notch, by using BrowserStack to take this automation process to the cloud. BrowserStack is a cloud web and mobile testing platforms that lets developers access browsers on-demand for their testing needs.
Cloud-based automation and testing have several benefits. It offers better collaboration, quicker and cost-effective testing, unlimited availability and some major advantages like customisation, parallelisation and support for agile workflows.
For this article, we will be using a sample Google form.
The first and the most obvious step would be to install Selenium on your system by using the following command in your terminal:
sudo pip3 install selenium
The next step would be to create a BrowserStack account (If you have one already, then that’s great). BrowserStack’s Automate Mobile Plan for 1 parallel and 1 user for 1 year is free for students through the Github Student Developer Pack.
You would then need to head to this page on BrowserStack’s website. Here you can select the browser and OS combination of your preference. After you’ve selected your preference, you would then need to create a new .py file on your system in a code editor. Let’s say, you wish to perform my test on Chrome 84 Browser on Mac OS X Catalina. So you should write down the following code in your .py file:
The first four lines of the code specify the imported libraries. The time library will be used later on in the project. The desired cap dictionary specifies the specifications of the OS and Browser to be used. The next statement ( webdriver.Remote() ) connects the program to the BrowserStack’s system. The unique string to be passed to the command_executor parameter would be found on this page when you select your Browser and OS preference. The access key is unique to each BrowserStack user and so is the link.
After this, you need to add the following python code to your .py file.
The driver.get() statement will open the link provided in the browser. The screenshot of the form is attached below.
Now your next step would be to select the correct options for each of the questions/fields. There are several ways to do this, but let’s say you use the XPath to select your desired option. For this, you need to hover over to the option of your choice and press the mouse’ right-click over the radio button/checkbox. Then you would need to select the inspect option. When you right-click on the highlighted element inside the new pane on the right, you would be able to copy the XPath, as shown in the picture below.
This works for multiple-choice, text, and checkbox fields. Now you can paste the XPath in the .py file in the form a string as shown below. You would also need the XPath of the Submit button and Submit another response (resubmit) button. The resubmit button makes automation easier.
Now, you are left with the final step, implementing the loop for automation. Let’s say you want to fill the form 100 times. So you run a while loop 100 times, incrementing variable i each time. Inside the while loop you use find_element_by_xpath and click() attribute to implement your functionality, For the text field, the send_keys method does the job. The time.sleep(1) waits for 1 second after submitting the form to avoid any unwanted error. The following code uses try and except for the same purpose (as a precaution). If there is no error, the tool will go on to submit another response in the same window. If an error is generated, it will close that window, and start with a new one. The driver.quit() statement at the end stops/completes the process of automation.
Now all you need to do is go to your terminal and use the following command to run the program.
Now you can move to your BrowserStack Dashboard and see your program fill the form 100 times.
Selenium is a powerful tool which can be used for various purposes. The code is easy to implement and it makes testing and automation a lot easier. BrowserStack provides an easy-to-use powerful interface which makes cloud-based testing a lot easier.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course