Wednesday, December 25, 2024
Google search engine
HomeLanguagesGoogle Maps Selenium automation using Python

Google Maps Selenium automation using Python

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

Google Maps is a popular web mapping service that provides satellite imagery, street maps, 360-degree panoramic views, and route planning for travel by foot, car, bicycle, or public transportation. You can use Selenium in Python to automate various tasks on Google Maps, such as searching for a location, getting directions, and zooming in/out. Here’s an example code snippet for automating Google Maps using Selenium in Python:

Using Selenium to automate Google Maps provides several benefits, including:

  1. Time-saving: Automating repetitive tasks on Google Maps can save you a lot of time and effort.
  2. Accuracy: Automation can reduce human error and increase the accuracy of the tasks performed.
  3. Flexibility: With Selenium, you can customize your automation according to your needs and preferences.

Some potential drawbacks of using Selenium for Google Maps automation include:

  1. Maintenance: As websites and applications change over time, your automation scripts may require maintenance and updates.
  2. Limitations: Some tasks may not be automatable due to limitations in the website or application.
  3. Complexity: Automating complex tasks may require advanced coding skills and knowledge of 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
from selenium.webdriver.common.by import By


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()
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 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 click on 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 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 into 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 the 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
from selenium.webdriver.common.by import By
 
 
# assign url in the webdriver object
driver = webdriver.Chrome()
driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")
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:

RELATED ARTICLES

Most Popular

Recent Comments