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
Download Google Image Using Python and Selenium In this article, we are going to see how to download google Image using Python and Selenium. Installation On the terminal of your PC, type the following command. If it triggers any error regarding pip then you need to 1st install pip on windows manually by python get-pip.py command then you can run
3 min read
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. Mastering Selenium will help you automate your day to da
3 min read
Search Google Using Python Selenium Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. This art
1 min read
How to Add Chrome Extension using Python Selenium IntroductionSelenium is a tool for browser automation that supports multiple browsers for testing. With webdriver we can use multiple languages like Python, C#, and Java to write the code for automation testing. For Adding a Chrome extension in a browser with Selenium WebDriver allows us to automate
5 min read
How to automate google Signup form in Selenium using java? For any QA engineer or developer, automating the Google Signup form with Selenium may be a hard nut to crack. Also, as the needs are increasing toward automated testing, in this article, we will learn how to deal with a complicated web form like Google Signup. We will show you how to automate the Go
4 min read
Python - Opening multiple tabs using Selenium Testing is an important concept in software methodology. Software is said to be effective and efficient only if it is bug-free. Testing can be done manually and also via automation. In Python, selenium is used to do automated testing. The selenium package is available, and they are much helpful to a
4 min read
Get all text of the page using Selenium in Python As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient A
3 min read
How to make a Google Translation API using Python? Google Translate is a free multilingual translation service, based on statistical and neural machine translation, developed by Google. It is widely used to translate complete websites or webpages from one languages to another. We will be creating a python terminal application which will take the so
4 min read
Automated Browser Testing with Edge and Selenium in Python Cross-browser testing is mandatory in the software industry. We all know that there are many browsers like Firefox, Chrome, Edge, Opera etc., are available. Rather than writing separate code to each and every browser, it is always a decent approach to go towards automated testing. Let us see how to
5 min read
How to Locate Elements using Selenium Python? Selenium: is an open-source tool that automates web browsers. It provides a single interface that lets you write test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others. I personally prefer Python as itâs very easy to write code in python. A browser-dri
3 min read