Google Maps Selenium automation using Python
Last Updated :
22 Sep, 2023
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:
- Time-saving: Automating repetitive tasks on Google Maps can save you a lot of time and effort.
- Accuracy: Automation can reduce human error and increase the accuracy of the tasks performed.
- 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:
- Maintenance: As websites and applications change over time, your automation scripts may require maintenance and updates.
- Limitations: Some tasks may not be automatable due to limitations in the website or application.
- 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:
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read