Create your Browser Automation Robot with Python and Selenium
More and more business processes are being automated. In a previous blog post, I explained how to get started with the automation of robotic processes using the Python Pyautogui module. But for web browser automation scripts, you can take an extra look at the Selenium module. This will allow you to automate boring and repetitive tasks on the Internet, for example a huge time saving on data entry in the CMS.
The advantage of the Selenium module is that it knows how to interpret html code. Your robot can easily navigate in and between web pages. Another advantage of Selenium over Pyautogui is that it does not take control of your mouse or keyboard, but is a process that takes place in the background.
As you can see in the video below, the robots are really good at performing specific tasks. So let’s use the power of robots.
Getting started with Selenium in Python
- Installing, importing and starting the driver
First install Selenium and import the webdriver. This code will launch the Google Chrome browser
!pip install selenium
!apt install chromium-chromedriver
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(‘ — headless’)
chrome_options.add_argument(‘ — no-sandbox’)
chrome_options.add_argument(‘ — disable-dev-shm-usage’)
2. Open a webpage
This code will open the url and print the source code of the URL.
3. Selecting elements on the page
You can use element ID or xpath to select form fields, buttons, hyperlinks, actually all elements on the page.
Does your element has a specific html ID? Then you can use
Otherwise you need to make use of XPath to select your element. First right You can find an XPath in the developer console as you see below. Select Copy XPath. The XPath of the search field is //*[@id=’tsf’]/div/div/div/div/div/input
xpathsearch = "//*[@id='tsf']/div/div/div/div/div/input"
searchinput = driver.find_element_by_xpath(xpathsearch)
Ok, now we selected the search element on Google.com. Let’s try making a Google search with Selenium.
4. Keyboard input
Once you selected an element you can manipulate this element with your keyboard and your mouse.
Use the send_keys function to write your search text tothe search field on Google.com and the ENTER key to start the search.
searchinput.send_keys("Barack Obama age")
5. Reading the result
Once you executed the search, you can read the result by using the XPath of the div which contains the result. You can read the text property of the element.
xpathresult = "//*[@id='rso']/div/div/div/div/div/div/div/div/div/div/div"
resultdiv = driver.find_element_by_xpath(xpathresult)
age = resultdiv.text
print("Barack Obama is getting old. Currently, his age is",age)
Caution: Google doesn’t like bots and may block your script when you execute it multiple times in a row.
Case: Automating CMS input
It’s time to give a practical example. Suppose you have written a bunch of meta descriptions in a spreadsheet and sent them to your client. Your client likes the new meta descriptions, and may suggest that you enter them into the CMS.
You can now do this manually, one by one, but it’s not the most exciting task. Python and Selenium can come to the rescue.
In this Colab notebook, you’ll find the code to automate the entry of meta descriptions in the Drupal CMS.
As you can see, it only took a few lines of code. By automating this process, you save time and become more efficient.
October 5, 2021
February 10, 2021
February 4, 2016