Web Crawling Webinar for Tech Teams
Web Crawling Webinar for Tech Teams

How to Take a Screenshot With Playwright in 2025

Updated: May 24, 2024 · 8 min read

Are you web scraping or testing a website with Playwright and want to take screenshots?

You're in the right place. In this article, you'll learn the three main methods of capturing screenshots with Playwright in Python:

Let's go!

How to Take a Screenshot With Playwright

Playwright is an open-source framework built on Node.js. It helps automate web browsing tasks and is known for its headless browser mode, which gives the tool a significant speed advantage. One of Playwright's key functionalities is screenshot-taking. Automated screenshotting has many commercial use cases, most notably in testing.

Depending on the part of the page you need to screenshot while scraping with Playwright, you will use a different method. In this section, you'll learn three ways of screenshotting different parts of a product page using ScrapingCourse.com as a demo web page.

Option 1: Generate a Screenshot of the Visible Part of the Screen

Grabbing the visible part of the screen is the easiest way to take a screenshot in Playwright. It only captures the section above the fold (the part of the page that appears immediately after loading). 

Below, you can see an example of a screenshot made with this method:

ScrapingCourse product page visible part screenshot
Click to open the image in full screen

Now, let's learn how to capture this type of screenshot.

First, start a synchronous Playwright chain and launch a new page instance:

scraper.py
# import the required library
from playwright.sync_api import sync_playwright

# initialize Playwright in synchronous mode
with sync_playwright() as p:

    # launch the browser
    browser = p.chromium.launch()

    # create a new page instance
    page = browser.new_page()

Visit the target website and initiate the screenshot method from the page instance. Then, close the browser:

File
# initialize Playwright in synchronous mode
with sync_playwright() as p:

    #   ...   

    # navigate to the target web page
    page.goto('https://www.scrapingcourse.com/ecommerce/product/abominable-hoodie/')

    # take a screenshot
    page.screenshot(path='visible-part-of-the-page.png')

    # close the browser
    browser.close()

Merge everything. Your final code should look like this:

scraper.py
# import the required library
from playwright.sync_api import sync_playwright

# initialize Playwright in synchronous mode
with sync_playwright() as p:

    # launch the browser
    browser = p.chromium.launch()

    # create a new page instance
    page = browser.new_page()

    # navigate to the target web page
    page.goto('https://www.scrapingcourse.com/ecommerce/product/abominable-hoodie/')

    # take a screenshot
    page.screenshot(path='visible-part-of-the-page.png')

    # close the browser
    browser.close()

Awesome! The code above will screenshot the visible part of the target page. You'll find the output in your project directory.

Next, let's increase your coverage to a full page. 

Frustrated that your web scrapers are blocked once and again?
ZenRows API handles rotating proxies and headless browsers for you.
Try for FREE

Option 2: Capture a Full-page Screenshot

Playwright lets you capture an entire web page, including the parts that require scrolling. For example, here's a full-page screenshot of the target product page:

ScrapingCourse product page full screenshot
Click to open the image in full screen

To get the same result, you need to add a full_page argument to the screenshot method and set its condition to True:

scraper.py
# import the required library
from playwright.sync_api import sync_playwright

# initialize Playwright in synchronous mode
with sync_playwright() as p:

    # launch the browser
    browser = p.chromium.launch()

    # create a new page instance
    page = browser.new_page()

    # navigate to the target web page
    page.goto('https://www.scrapingcourse.com/ecommerce/product/abominable-hoodie/')

    # take a full-page screenshot
    page.screenshot(path='scrapingcourse-page-full-screenshot.png', full_page=True)

    # close the browser
    browser.close()

Yes, it's this easy! Now you know how to screenshot an entire page with Playwright.

Option 3: Create a Screenshot of a Specific Element

If you only want to screenshot a selected part of a page, Playwright also lets you capture its specific element. To do that, you'll have to point your web scraper to the target element.

For demonstration, let's grab a screenshot of the product summary container. It will look like this:

ScrapingCourse specific element screenshot.
Click to open the image in full screen

First, start Playwright in synchronous mode and launch a page instance:

scraper.py
# import the required library
from playwright.sync_api import sync_playwright

# initialize Playwright in synchronous mode
with sync_playwright() as p:

    # launch the browser
    browser = p.chromium.launch()

    # create a new page instance
    page = browser.new_page()

Next, open the web page and obtain the target element using its class attribute (.entry-summary). Then, screenshot it into the specified path and close the browser:

scraper.py

with sync_playwright() as p:
  
    # ...
    
    # navigate to the target web page
    page.goto('https://www.scrapingcourse.com/ecommerce/product/abominable-hoodie/')
    
    # obtain the target element
    element = page.query_selector('.entry-summary')

    # grab a screenshot of the target element
    element.screenshot(path='scrapingcourse-specific_element_screenshot.png')

    # close the browser
    browser.close()

Here's the complete code:

scraper.py
# import the required library
from playwright.sync_api import sync_playwright

# initialize Playwright in synchronous mode
with sync_playwright() as p:

    # launch the browser
    browser = p.chromium.launch()

    # create a new page instance
    page = browser.new_page()

    # navigate to the target web page
    page.goto('https://www.scrapingcourse.com/ecommerce/product/abominable-hoodie/')

    # obtain the target element
    element = page.query_selector('.entry-summary')

    # grab a screenshot of the target element
    element.screenshot(path = 'scrapingcourse-specific_element_screenshot.png')

    # close the browser
    browser.close()

The code will return the expected screenshot. Congratulations!

Keep in mind that some websites implement anti-bot systems to block scrapers. How can you bypass them and grab the screenshot you want?

Avoid Getting Blocked While Taking Screenshots

Modern websites actively block bot access, including for screenshot attempts. Without proper anti-bot bypassing, your screenshot automation will fail before it begins.

Let's try taking a screenshot from a protected page like the Antibot Challenge page with Playwright:

scraper.py
# import the required library
from playwright.sync_api import sync_playwright

# initialize Playwright in synchronous mode
with sync_playwright() as p:

    # launch the browser
    browser = p.chromium.launch()

    # create a new page instance
    page = browser.new_page()

    # navigate to the target web page
    page.goto('https://www.scrapingcourse.com/antibot-challenge')

    # take a full-page screenshot
    page.screenshot(path='anti-bot-page-screenshot.png', full_page=True)

    # close the browser
    browser.close()

Running this code results in getting blocked. So, instead of your desired screenshot, you'll see this:

scrapingcourse cloudflare blocked screenshot
Click to open the image in full screen

The best solution to avoid getting blocked is to use a web scraping API like ZenRows. It's an all-in-one web scraping solution that helps you fix your request headers, auto-rotate premium proxies, and bypass any anti-bot system. Also, it comes with all the functionality of a headless browser like Playwright.

Start by signing up for a new account, and you'll get to the Request Builder.

building a scraper with zenrows
Click to open the image in full screen

Paste the target URL, enable JS Rendering, and activate Premium Proxies.

Next, select Python and click on the API connection mode. Then, copy the generated code and paste it into your script.

scraper.py
# pip3 install requests
import requests

url = "https://www.scrapingcourse.com/antibot-challenge"
apikey = "<YOUR_ZENROWS_API_KEY>"
params = {
    "url": url,
    "apikey": apikey,
    "js_render": "true",
    "premium_proxy": "true",
}
response = requests.get("https://api.zenrows.com/v1/", params=params, print(response.text)

The generated code uses Python's Requests library as the HTTP client. You can install this library using pip:

Terminal
pip3 install requests

Paste the code into your IDE, and change the response type to stream in order to read the generated image.

scraper.py
# ...

# send the request and set the response type to stream
response = requests.get('https://api.zenrows.com/v1/', params=params, stream=True)

# check if the request was successful
if response.status_code == 200:

    # save the response content as a screenshot
    with open('anti-bot-page-screenshot.png', 'wb') as f:
        f.write(response.content)
    print('Screenshot saved successfully.')
else:
    print(f'Error: {response.status_code}')

Your complete code should look like this:

scraper.py
# import the required library
import requests

# specify the query parameters with a screenshot option
params = {
    'url': 'https://www.scrapingcourse.com/antibot-challenge',
    'apikey': '<YOUR_ZENROWS_API_KEY>',
    'js_render': 'true',
    'premium_proxy': 'true',
    'return_screenshot': 'true'
    }

# send the rquest and set the response type to stream
response = requests.get('https://api.zenrows.com/v1/', params=params, stream=True)

# check if the request was successful
if response.status_code == 200:

    # save the response content as a screenshot
    with open('anti-bot-page-screenshot.png', 'wb') as f:
        f.write(response.content)
    print('Screenshot saved successfully.')
else:
    print(f'Error: {response.status_code}')

The code captures a screenshot of the protected page.

scrapingcourse-antibot-challenge-passed
Click to open the image in full screen

Congratulations! You used ZenRows to access a protected page and captured a screenshot.

Refer to our official documentation to learn more.

Conclusion

While Playwright provides basic screenshot capabilities, it's not sufficient for capturing screenshots on many sites at scale. Websites employ various anti-bot measures, such as browser fingerprinting, behavioral analysis, and request pattern monitoring.

ZenRows handles all these challenges automatically. It provides automatic proxy rotation, JavaScript rendering, CAPTCHA bypass, and everything you need to avoid getting blocked, making it the ideal choice for taking screenshots from any website. Try taking screenshots with ZenRows for free.

Ready to get started?

Up to 1,000 URLs for free are waiting for you