Prerequisites: Browser Automation using Selenium
Selenium is a powerful tool for controlling a web browser through the program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. It can be installed using the below command:
pip install selenium
In this article, we are going to see how to automate the Google Maps search using selenium by getting the location of a place and its transportation details to another location.
Step 1) Import modules
Python3
# import required modules from selenium import webdriver from time import sleep |
Step 2) Mention the path of your chrome driver in the driver variable that you downloaded. And then we need to get a Google Maps website.
Python3
# assign url in the webdriver object driver = webdriver.Chrome() sleep( 2 ) |
Step 3) Next step Declare a function under this function you need to inspect the search bar on the Google maps website. And Copy the Class name in the variable place. You need to send keys to a particular web element. Give your destination as an input. Provide the xpath value in the submit variable this is used to press the search button
Python3
# search locations def searchplace(): Place = driver.find_element_by_class_name( "tactile-searchbox-input" ) Place.send_keys( "Tiruchirappalli" ) Submit = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[1]/div[1]/div[2]/div[1]/button" ) Submit.click() searchplace() |
Step 4) Declare a function called directions. Under this function, you need to send a click to the direction button. Copy the X path value of the directions button from the Google maps website. And paste the value in the variable.
Python3
# get directions def directions(): sleep( 10 ) directions = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div/button" ) directions.click() directions() |
Step 5) Declare a function called as find. Under this function, you need to create a variable and in this variable, you need to copy the xpath of the value of the search bar and paste the value in the variable.
Next, Send the starting point to the particular search box. And You need to send a click button to the search button to follow the Step 1 Process.
Python3
# find place def find(): sleep( 6 ) find = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/div/input" ) find.send_keys( "Tirunelveli" ) sleep( 2 ) search = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/button[1]" ) search.click() find() |
Step 6) Now we need to scrap the essential data to complete our automation process. Here we need to copy the xpath values of Total kilometers between two places
Bus travel time and Train travel time between these two places. Here I extracted the data from the Google maps website by using the Web-Elements.
Python3
# get transportation details def kilometers(): sleep( 5 ) Totalkilometers = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[2]/div" ) print ( "Total Kilometers:" , Totalkilometers.text) sleep( 5 ) Bus = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[1]/span[1]" ) print ( "Bus Travel:" , Bus.text) sleep( 7 ) Train = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[2]/div[1]/div[2]/div[1]/div" ) print ( "Train Travel:" , Train.text) sleep( 7 ) kilometers() |
Below is the complete program based on the above approach:
Python3
# import required modules from selenium import webdriver from time import sleep # assign url in the webdriver object driver = webdriver.Chrome() sleep( 2 ) # search locations def searchplace(): Place = driver.find_element_by_class_name( "tactile-searchbox-input" ) Place.send_keys( "Tiruchirappalli" ) Submit = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[1]/div[1]/div[2]/div[1]/button" ) Submit.click() searchplace() # get directions def directions(): sleep( 10 ) directions = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div/button" ) directions.click() directions() # find place def find(): sleep( 6 ) find = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/div/input" ) find.send_keys( "Tirunelveli" ) sleep( 2 ) search = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/button[1]" ) search.click() find() # get transportation details def kilometers(): sleep( 5 ) Totalkilometers = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[2]/div" ) print ( "Total Kilometers:" , Totalkilometers.text) sleep( 5 ) Bus = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[1]/span[1]" ) print ( "Bus Travel:" , Bus.text) sleep( 7 ) Train = driver.find_element_by_xpath( "/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[2]/div[1]/div[2]/div[1]/div" ) print ( "Train Travel:" , Train.text) sleep( 7 ) kilometers() |
Output:
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.