Google Maps Selenium automation using Python - GeeksforGeeks

04 Jan, 2021
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 modulesfrom selenium import webdriverfrom 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 objectdriver = webdriver.Chrome()driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")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 locationsdef 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 directionsdef 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 placedef 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 detailsdef 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 modulesfrom selenium import webdriverfrom time import sleep    # assign url in the webdriver objectdriver = webdriver.Chrome()driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")sleep(2)    # search locationsdef 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 directionsdef 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 placedef 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 detailsdef 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:
Python Selenium-Exercises
Python-selenium
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()