Python 3 Selenium Script to Calculate Google Directions Between Two Addresses and Save as PNG Image
406 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-selenium-script-to-calculate-google-directions-between-two-addresses-and-save-as-png-image/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a selenium
0:05
python script which will uh actually
0:08
take uh the screenshot of uh the Google
0:12
map so between two locations it will
0:15
show you the directions which directions
0:17
you want to travel if you want to travel
0:20
from one location to another so this is
0:22
actually the Python script right here so
0:24
if I execute this Python
0:26
script so let me just show you the demo
0:28
right here so as I execute this you will
0:31
see it will ask you to input the
0:34
starting address so you need to enter
0:37
the source address and the destination
0:39
so let's suppose I provide any address
0:42
right
0:44
here and then the
0:52
destination uh let me just restart
1:04
this so as soon as you enter your
1:06
starting and the destination address
1:09
then automatically selenium will open in
1:11
the background it will open Google maps
1:14
website it will take the screenshot and
1:16
uh then it will save this as a image
1:19
file jpg image file you will see
1:21
direction screenshot has been
1:24
saved so you can
1:27
see it has taken that image
1:29
automatically and you can see the
1:32
directions if you want to travel from
1:34
this location to this
1:36
location it it just shows you the full
1:40
route here full
1:42
directions this image it also tells you
1:45
the amount of time which will take to
1:48
travel from this source to destination 1
1:50
hour 20
1:51
minute so all sort of information is
1:54
there so this is happening using
1:57
selenium which is a automation library
2:00
inside Python so first of all you just
2:04
need to install this so the command is
2:06
very simple just search for this package
2:09
here and uh the command is simple simply
2:13
install this by executing this command
2:15
i've already installed it so I've given
2:18
this full script in the description of
2:20
this video so if you need the full
2:23
script here uh the link is given in the
2:25
description so now let's get started so
2:27
let me show you the script here so first
2:29
of all you need to import the necessary
2:31
packages so just need to write here from
2:34
selenium we need to import the web
2:36
driver and then again from the same
2:39
package selenium we need to import this
2:43
dot chrome
2:46
options from this we just need to import
2:49
the
2:51
options once again from selenium webd
2:54
driver
2:57
common we need to import by and then
3:01
lastly we also need to import the time
3:03
module as well so these are all the
3:06
packages which are needed so after
3:08
importing these packages now we just
3:10
need to actually receive this input from
3:13
the user we will use the input function
3:16
so here we will ask the user to enter
3:18
the starting
3:23
address and then the destination address
3:26
once again we will ask the question so
3:29
here the user will enter the
3:35
address so after that we will actually
3:38
call a function custom functions that I
3:41
will define so here we pass the starting
3:44
index starting address and the ending
3:46
address so now we just need to define
3:49
this function
3:58
so now inside this function we need to
4:00
initialize selenium uh
4:03
library so the third argument is
4:07
actually the output file name the image
4:10
file name so you can give it a default
4:12
name which will be directions screenshot
4:15
dot jpg it can be jpg or png let me save
4:19
it as
4:20
jpg so this will be your starting
4:23
address and this will be your ending
4:25
address which will be passed so now
4:28
inside this function the very first
4:29
thing we need to do we need to
4:31
initialize the chrome
4:36
options we can initialize like this and
4:39
after that we just need to add some
4:40
argument here using this function so if
4:44
you want to
4:48
uh just do this task in the background
4:50
then you can add this option of
4:53
headless but if you want to see the
4:56
browser opening then you can remove this
4:59
option but I want the jog to be
5:02
happening in the background so you can
5:04
add this option headless after that you
5:07
just need to add another argument here
5:09
which will be
5:10
d-wind and here you can specify the size
5:13
of the window so I will say 1280 by
5:16
720 it's a normal size of the window and
5:20
after that we just need to initialize
5:22
the selenium driver so for initializing
5:24
it we simply say webdriver dot chrome
5:27
and inside this we basically pass the
5:30
options that we define here which is
5:31
chrome
5:33
options after defining this uh now we
5:37
will actually write the code in try
5:40
catch block so inside this we
5:44
will receive this address and replace
5:48
this by using this replace
5:50
function so this we need to do for both
5:54
the starting and the ending address that
5:56
we are
5:59
receiving so like
6:02
this so then we need to construct the
6:07
URL
6:09
so we need to open this URL in the
6:13
selenium browser which will be www
6:16
google.com/maps
6:19
and then here we need to specify the
6:22
starting address and the ending
6:25
address so now we need to open this URL
6:28
programmatically for this we just need
6:30
to call this get function we go to this
6:33
URL and then we sleep for 6 seconds so
6:36
that we can take the screenshot so the
6:39
sleep method will actually pause on that
6:43
page for 6 seconds to take the
6:45
screenshot
6:47
so after it takes the
6:51
screenshot again we need to write this
6:54
in the try block here so here we simply
6:58
need to find this element so for finding
7:01
the element in the page we have this
7:02
method and here we specify by
7:07
id so here we just need to provide this
7:11
id so this will wait for the DOM element
7:14
to load and then it will click on that
7:17
element
7:18
automatically so this is actually the
7:20
power of selenium library it can do the
7:22
task automatically for you so then we
7:25
just need to wait for on for 1
7:29
second and if the element is not there
7:31
then we can simply say accept
7:35
pass then we take the
7:38
screenshot and after taking the
7:40
screenshot we can save the screenshot on
7:43
the local machine by we have this
7:45
function save screenshot and we pass the
7:49
location output
7:57
file and then we can simply write a
8:00
print statement that your screenshot has
8:02
been successfully saved so
8:08
uh that's all that we need to do and we
8:10
just need to finally quit the driver so
8:14
we will have the finally block which
8:15
will here be we simply quit the selenium
8:18
driver so this completes the application
8:21
so it's a very simple application I
8:23
showed you step by step so again if I
8:26
execute this you will see it will ask
8:28
you the starting address
8:32
so here you can specify any address and
8:35
then the destination
8:40
address so now the window will
8:45
open so it will take the screenshot
8:55
so if I see the screenshot here you will
8:58
see it will exactly tell you the
9:01
distance how much time it will take it
9:04
will draw the complete route and the
9:07
directions which needs to so here this
9:11
is your source address this is your
9:12
destination address so for this we can
9:16
see we are not using any sort of API for
9:18
this so this is simply we are navigating
9:21
through the uh URL simply pausing it for
9:24
6 seconds taking the screenshot and
9:26
saving it inside your local machine so
9:30
so this is actually the selenium script
9:31
for this uh the link is given in the
9:34
description you can actually copy the
9:36
script and also check out my website
9:39
which is freemediatools.com
9:41
uh which contains thousands of tools