Cloud-based Automation using Selenium in Python and BrowserStack

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
  
desired_cap = {
 'browser': 'Chrome',
 'browser_version': '84.0 beta',
 'os': 'OS X',
 'os_version': 'Catalina',
 'resolution': '1024x768',
 'name': 'Bstack-[Python] Sample Test'
}
  
driver = webdriver.Remote(
    command_executor ='Your unique BrowserStack Access Key link',
    desired_capabilities = desired_cap)

chevron_right


 
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

chevron_right


The driver.get() statement will open the link provided in the browser. The screenshot of the form is attached below.

The sample form used for this article.

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.

Copying XPath

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

radioOption3 ='//*[@id ="mG61Hd"]/div[2]/div / div[2]/div[1]/div / div / div[2]/div[1]/div / span / div / div[3]/label / div / div[1]/div / div[3]'
checkBox2 = '//*[@id ="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div[1]/div[2]/label/div/div[1]/div[2]'
checkBox3 = '//*[@id ="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div[1]/div[3]/label/div/div[1]/div[2]'
text = '//*[@id ="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input'
submit = '//*[@id ="mG61Hd"]/div[2]/div/div[3]/div[1]/div/div/span/span'
  
resubmit = '/html / body / div[1]/div[2]/div[1]/div / div[4]/a'

chevron_right


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. 

filter_none

edit
close

play_arrow

link
brightness_4
code

i = 0
while i<100:
    driver.find_element_by_xpath(radioOption3).click()
    driver.find_element_by_xpath(checkBox2).click()
    driver.find_element_by_xpath(checkBox3).click()
    text_box = driver.find_element_by_xpath(text)
    text_box.send_keys("The text I need")
    driver.find_element_by_xpath(submit).click()
    i = i + 1
    time.sleep(1)
    try:
        driver.find_element_by_xpath(resubmit).click()
    except:
        driver.close()
        driver = webdriver.Remote(command_executor ='Your unique BrowserStack link', desired_capabilities = desired_cap)
        driver.get('https://forms.gle / xX5NEumdXW4Z7SrC6')
driver.quit()

chevron_right


Now all you need to do is go to your terminal and use the following command to run the program.

python3 filename.py

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.