Python 3 Selenium Script to Take Screenshot of Website URL & Save it as PNG Image & PDF File
2K views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-selenium-script-to-take-screenshot-of-website-url-save-it-as-png-image-pdf-file/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a Python
0:04
script which will actually take the
0:06
screenshot of a website in both a PNG
0:10
image and also the PDF for this purpose
0:12
we are using a selenium library which is
0:14
a very popular Python package so just
0:18
search for this library selenium so it's
0:22
official selenium library simply execute
0:24
this command to install this package
0:26
which I have already installed
0:28
it and this is my Python script here so
0:32
if I run this Python script it will go
0:34
to this website google.com it will take
0:36
the screenshot in both image and PDF
0:39
format so if I run this script right
0:40
here
0:42
pythonapp.py so it will all happen in
0:44
the background you will see it will open
0:46
the browser automatically it will take
0:48
the screenshot so you will see it has
0:51
taken the screenshot in PNG and PDF
0:53
format if you check now the both the
0:56
files have been created so you can see
0:59
it has taken the screenshot of
1:01
google.com and this is the PDF
1:05
document so this is the Python script
1:07
that I will show you the script is given
1:10
in the description of the video you can
1:11
check out the full script so you can
1:14
change this to any website let's suppose
1:16
I say
1:17
YouTube.com so once again it will repeat
1:20
the same process once again it will open
1:22
the browser in the background it will go
1:23
to youtube.com and take the
1:27
screenshot image and PDF so you can see
1:31
now it has taken uh this
1:34
website same goes with the PDF as well
1:38
so let me show you the for this you need
1:41
to have a selenium chromedriver this
1:45
present in the same directory so you can
1:47
actually go to this website what is my
1:50
browser it will detect which version you
1:53
are using so I'm using Chrome 136 so for
1:56
this you need to have Selonium Chrome
1:59
driver
2:01
simply search this on Google and simply
2:05
go to this website and download your
2:07
supported chromedriver depending upon
2:10
which version you're using so simply
2:12
store this inside the directory on which
2:14
you are developing the application
2:17
so now let me show you step by step just
2:21
create a simple app py file and first of
2:25
all we need to
2:27
import the selenium package so we will
2:30
simply say from selenium we need to
2:33
import the web driver and from selenium
2:38
webdriver dot chrome dot options we need
2:42
to import the
2:43
options and also from
2:48
selenium dot webd driver dochrome dots
2:52
service we need to import the service
2:54
package as well so here we are importing
2:56
all the dependencies that we need for
2:58
this project apart from that we also
3:01
need the time
3:02
module and the base 64 module as well
3:05
and the operating system so these are
3:08
all the modules that we need for this
3:10
application we imported this after that
3:12
we just need
3:14
to
3:16
simply pass the selenium driver so it is
3:19
present in the same directory so I will
3:22
simply pass like
3:30
this so here we need to specify the
3:33
chromedriver
3:35
path so in this current directory comma
3:39
and chromedriver dot this is really
3:43
important you need to pass this selenium
3:45
chromedriver without this the
3:46
application will not work so simply
3:49
store this inside the same directory and
3:51
pass it inside the script so after that
3:54
we just need to set some options for the
3:57
chrome
3:58
browser so let me call this as chrome
4:03
options and we will initialize it
4:07
and and add argument so first argument
4:12
will be headless headless simply means
4:15
that it will work in the background you
4:18
can't see the opening and closing of
4:20
browser apart from that we will also set
4:24
these four
4:27
options it will disable GPU node sandbox
4:31
window size will be 1280 by 720 so these
4:34
are all the
4:36
options and then we need to specify the
4:39
service
4:42
so we'll use the service constructor and
4:44
we will simply pass the executable
4:48
path which is the chromedriver
4:51
path after that we will initialize the
4:54
driver webdriver dot chrome and inside
4:57
this you pass your
5:00
service and then the
5:04
options so after initializing it we will
5:07
simply tell the URL which URL you need
5:10
to go to so I will say http googleg
5:12
google.com and then we will open this
5:14
URL by this function get and then we
5:18
will wait for 2 seconds so for waiting
5:20
it we have this method of time do sleep
5:24
so it will pause and wait on that web
5:27
page for 2 seconds so that we can take
5:30
the
5:31
screenshot and now for taking the
5:34
screenshot we have this method driver
5:36
dots save screenshot and here you simply
5:39
say
5:42
screenshot.png and we simply say saved
5:46
screenshot.png so this is the overall
5:48
script for taking the screenshot in the
5:50
png file so if you just execute your
5:55
application
6:00
now you will see the browser will open
6:03
it will take the screenshot wait for 2
6:06
seconds
6:08
so you will see your screenshot has been
6:10
successfully taken in PNG file so now
6:13
for also taking the screenshot in PDF a
6:16
little bit more code you need to write
6:18
here after that for generating the PDF
6:21
we simply have to say
6:24
here PDF data and driver do.execute
6:30
execute cdp cmd and here we simply say
6:34
page dot print to
6:40
PDF and in the second argument it takes
6:43
an object here which is
6:45
print
6:47
background to true so this will change
6:50
the image to PDF and now for saving the
6:52
PDF file we use the open function and
6:55
specify the name of the PDF file so
6:57
screenshot PDF write bindary as f
7:02
F.Right to save the PDF file for saving
7:04
it we use the B 64 module and it
7:07
contains this function
7:09
B64 decode and here we specify PDF
7:16
data and then we write the simple
7:19
notification that uh your
7:22
PDF has been saved
7:27
and then we simply quit the application
7:31
driver.quit so this completes the script
7:34
here
7:35
and so once again if I execute this now
7:38
it will go to the URL it will wait for 2
7:41
seconds it will take the screenshot in
7:43
PNG and
7:46
PDF so you can see now it has taken the
7:49
screenshot
7:51
and uh if I show the PDF file here you
7:54
will
7:54
see and also the PNG so in this way guys
7:58
inside Python you can write this script
8:01
using Selenium library to take the
8:03
screenshot of any web page URL
8:08
website and take the screenshot in both
8:11
PDF and image so thank you very much for
8:13
watching this video and also check out
8:15
my website freemediatools.com
8:18
uh which contains thousands of tools
#Programming
#Scripting Languages