Pyppeteer Tutorial: Guide to Puppeteer in Python (with Examples)
What is Pyppeteer?Use cases of Pyppeteer
- What is Pyppeteer?
- Use cases of Pyppeteer
- Differences between Puppeteer and Pyppeteer
- How to set up Pyppeteer?
- How to execute several Actions on Pyppeteer
- Managing Cookies with Pyppeteer
- Handling iFrames with Pyppeteer
- Handling Alerts and Pop-ups with Pyppeteer
- Handling Dynamic Content with Pyppeteer
- Parallel Execution with Pyppeteer
- Cross Browser Testing with Pyppeteer
- Best Practices for using Pyppeteer
- How to run Pyppeteer Tests on Real Devices with BrowserStack Automate
Pyppeteer Tutorial: Guide to Puppeteer in Python (with Examples)
is the most popular mechanization testing tool by Google. However, it indorse only JavaScript programming language. Many Python enthusiasts and community contributors group to support Python for Puppeteer. Pypeeteer is a port of Puppeteer and it mimics the Puppeteer API by apply the Python programming language.
Overview
What is Pyppeteer?
Pyppeteeris an unofficial Python porthole of Puppeteer, a Node.js library, that automates Chromium-based browser through the DevTools Protocol.
Why Use Puppeteer?
- Automate UI testing for modernistic web covering
- Scrape JavaScript-heavy websites dynamically
- Generate PDFs or screenshots of webpage programmatically
- Simulate user interactions (detent, typing, navigation)
- Test website performance across device and networks
- Control headless browsers in CI/CD pipelines
- Monitor uptime and optic changes in web page
This usher explains what Pyppeteer is, its usage, how it differs from Puppeteer, stake practices and more.
What is Pyppeteer?
Pyppeteer is a Python version of the Puppeteer mechanisation tool. It helps to automatise the web covering apply the Python lyric. It continues to keep the core functionality like to Puppeteer. Pyppeteer supports Chromium and Chrome browsers for. It is an open-source package distributed under MIT license.
Read More:
Use cases of Pyppeteer
Here are some of the use cases of Pyppeteer:
1. Web Scraping Dynamic Content
- Extract data from JavaScript-heavy websites that don ’ t render content on the server side.
- Example: Scraping production listings, news clause, or job postings.
2. Automated Form Submission
- Fill and submit forms for tasks like testing, passel registrations, or bots.
- Example: Auto-fill contact forms, job applications, or view.
3. PDF Generation from Web Pages
- Convert webpage (including charts, tables, and style) into clean PDFs.
- Example: Invoice coevals, report exports, or gross.
4. and Monitoring
- Run end-to-end tests for frontend apps by imitate user interactions.
- Example: Test if login stream, search functionality, or checkout operation works.
5. Website Screenshot Automation
- Capture screenshots of full pages, specific elements, or responsive views.
- Example: Optical regression testing, content archiving, or prevue coevals.
6. Social Media or SEO Bot Tasks
- Automate posting, monitoring, or capturing metadata like rubric and description
- Example: Auto-post blog links or validate open graph ticket.
7. Emulating Devices and Networks
- Simulate different screen sizes, devices, or dumb network conditions.
- Example: Test mobile responsiveness or app behavior on 3G.
8. Capturing Performance Metrics
- Measure page load clip, resource usage, and script performance.
- Example: Benchmark web performance across deployments.
9. Captcha Pre-checks (Non-breaking Only)
- Detect front of CAPTCHA and log/report (not for clear).
- Example: Monitor website defenses or redirects.
10. Browser Fingerprint Emulation
- Customize user-agent, viewport, geolocation, timezone, etc.
- Example: Test personalization, localization, or bot detection mechanisms
Read More:
Differences between Puppeteer and Pyppeteer
Fundamentally, both are built on the same architecture. However, Pyppeteer is a ported version of Puppeteer JavaScript as there are programming-level limitation few things may change.
A few pyppeteer examples are listed below:
Puppeteer uses an target for passing parameters to functions. Pyppeteer functions accept both dictionary and keyword arguments for options
Example:
browser = await launch ({'headless ': True}) browser = await launch (headless=True)Puppeteer JavaScript uses the$for the element picker. However, in Python,$is not a valid identifier, instead of$python expend J as a tachygraphy operator as name below:
| Puppeteer | Pyppeteer | Pyppeteer stenography |
|---|---|---|
| Page.$() | Page.querySelector () | Page.J() |
| Page.$$() | Page.querySelectorAll () | Page.JJ() |
| Page.$x() | Page.xpath () | Page.Jx() |
Puppeteer & # 8217; s version of JavaScript evaluate () lead a JavaScript map or a string representation of a JavaScript expression. However, Pyppeteer lead a string representation of JavaScript or a function
Example:
content = await page.evaluate ('document.body.textContent ', force_expr=True)You experience discussed Pyppeteer and Puppeteer deviation understand the usage with the Pyppeteer example:
Note:Pyppeteer project is no longer actively maintained. The late version of Pyppeteer doesn ’ t employment smoothly, it is not recommended to use Pyppeteer rather as per the documentation it propose using Playwright Python which is well-maintained and more similar to Pyppeteer.
How to set up Pyppeteer?
Discuss installing and setting up a Pyppeteer using Python.
Pre-requisites to install Pyppeteer
- Basic understanding of Python
- Download and install Python 3.8 or higher
Install Pyppeteer
- Navigate to the desired brochure (exemplar, PyppeteerDemo)
- Open Terminal and type the below bidding
pip install Pyppeteer
Once the initiation is successful you are full to part the automation.
Read More:
How to perform various Actions on Pyppeteer
It is significant to cognise about the action that can be perform using the Pyppeteer. If you are already familiar with Puppeteer the learning curve will be very minimal. Understand the different action in Pyppeteer
Launching the browser with Pyppeteer
To launch the browser you involve to create the browser instance first.
SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.
browser = await launch ()
This can be used to create the browser instance or context. Using the browser object you can create as many Page as possible
Pyppeteer Example:
import asyncio from pyppeteer import launching async def scraper (): browser =await launching ({`` headless '': False}) page = await browser.newPage () await page.goto ('https: //www.google.com/ ') expect browser.close () asyncio.run (scraper ())In the above codification, a browser representative is create foremost and so name a newPage () method to get the new page. Once a new page is created, you can perform action like sail to the URL (example:https: //www.google.com), etc.
Read More:
Opening specific versions of Chrome/Chromium browser with Pyppeteer
Pyppeteer is more flexible and customizable, You can specify the browser executable to launch the browser. For example, if you have already installed a specific variation of Chrome you can pass the path to thelaunch()function as mentioned below
browser = await launching (headless=False, executablePath= ' C: \\Program Files\\Google\\Chrome\Application\\chrome.exe ')
Typing the Test on the web page
Consider you want to search for something on the page, you can use the page.type () method to execute the action
Example:
page.type (`` # mySearch '', `` Browserstack '');
In the above method, # mySearch is the locater and BrowserStack is the Search text
Clicking the Button on the Webpage
Pyppeteer get clicking buttons very simple,page.click ()method can be used to perform the click activeness.page.click ()method accepts one parameter that is the locator, the locator can be any valid css locator.
Example:
page.click (' # mybtn ');Printing PDF files with Pyppeteer
Pyppeteer let you to print or save the webpage as PDF, rather of taking a screenshot you can save the unhurt page in PDF format. Page.pdf () function can be used for this function.
Example:
await page.pdf ({'path ': 'python_print.pdf '})Switching Tabs with Pyppeteer
Pyppeteer follows a different access to switching the tabs. As mentioned earlier you can create as many as pages you want once you get the browser illustration. Understand with an example:
browser =await launch ({`` headless '': False}) page1 = await browser.newPage () page2 = await browser.newPage () expect page1.goto ('https: //www.google.com/ ') await page2.goto ('https: //www.browserstack.com/ ');In the above example, you have make two pages videlicet page1 and page2, page1 represents the first page and page2 represents the second page. You can just use page 1 or foliate 2 to perform the activity.
Example: Clicking on the hunt button on the 2nd page
await page2.click (' # browserstackSearchButtton ')Managing Cookies with Pyppeteer
Pyppeteer provides the capableness to contend the cookies. You can Set, Print, and Delete the cookies. Below are the available methods to manage cookies
Example:
await page.cookies (): Get all the available cookies await page.setCookie (... cookies): Set the Cookie await page.deleteCookie (... cookieNames): Delete the cookie
Read More:
Handling iFrames with Pyppeteer
IFrames are a legacy technique to dissever the page, still, Pyppeteer support iFrame actions.
For example, if you want to perform a click action on a specific element on the iFrame you can postdate the below access
Example:
iframe_element = await page.querySelector (iframe_1) iframe = await element.contentFrame () await iframe.querySelector (iframe_button) .click ()
Handling Alerts and Pop-ups with Pyppeteer
Alerts and pop-ups are native to the operating system. These can be handled using theasyncio()functionality. Below are example of handle the browser popups and alarum messages
Example: Handle Confirm Dialog Box
page.on ('dialog ', handle_confirm _dialog_box) def handle_confirm _dialog_box (dialogue): asyncio.ensure_future (dialog.accept (test_message))Example: Handle Dismiss Dialog Box
page.on ('dialog ', handle_dismiss_dialog_box) def handle_dismiss_dialog_box (dialog): asyncio.ensure_future (dialog.dismiss ())Similarly, you can care different types of Dialogs
Handling Dynamic Content with Pyppeteer
Dynamic web elements are a new standard of modern web applications. For example, when you navigate to the page it loads only contents or elements for the current viewport. As you scroll down additional substance are lend.
In such scenario, if you navigate to the web page and perform an activity it throws the exception with the error factor not found. One of the answer for this is to scroll down until webpage elements are visible.
This scenario can be handled in Pyppeteer utilize thescrollIntoView () function.
Example:
Scroll until the element is seeable async def scroll_to_element (page, picker): await page.evaluateHandle ('' 'async (chooser) = & gt; {const element = document.querySelector (selector); if (element) {element.scrollIntoView ();}} '' ', selector) return selectorOnce element is found perform the action
elem_button_footer = await scroll_to_element (page, button_footer) await page.click (elem_button_footer)
Read More:
Parallel Execution with Pyppeteer
Pyppeteer doesn & # 8217; t support parallel execution. That means you can not run multiple tryout at a single point of clip however this can be achieved using the third-party plugin calledpytest-parallel.
Once you install pytest-parallel you can use the following command to run the test in analog
- pytest & # 8211; workers 2:Run trial in 2 threads
- pytest & # 8211; workers auto:Automatically runs by looking at the CPU core
Web Scraping with Pyppeteer
Pyppeteer Python is a good combination for scraping web page. Web scrape helps to perform research in the industry, especially in the retail segment. Competitors make an analysis of particular products, their SKUs, pricing and discounts, etc.
Pyppeteer can be used for web scraping. Below is a simple example of web scraping.
- Below are the steps to execute
- Navigate to webpage
- Get all the product cards
- For each product get the name and damage
Example:
async def scrape_it (page, url) - & gt; leaning: page.goto ('https: //someretailwebsite.com ') rows = await page.querySelectorAll (' # product_cards ') scraping_data_arr = [] for row in rows: name = await row.querySelector (' p.name ') damage = await row.querySelector ('div.price-value ') nameText = await page.evaluate (' (element) = & gt; element.textContent ', name) priceValue = await page.evaluate (' (element) = & gt; element.textContent ', price) scraping_data_dict = {'product name ': nameText, 'product price ': priceValue} scraping_data_arr.append (scraping_data_dict) return scraping_data_arrCross Browser Testing with Pyppeteer
As discourse in the above subdivision, Pyppeteer is a fork of Puppeteer JavaScript. The browser support is the same as the Puppeteer. Puppeteer is intend to execute testing in browser just. It doesn & # 8217; t endorse any early browser.
Though it has experimental support for Firefox it is not recommended to use. Due to these limitations, can not be reach utilise Pyppeteer. However, the best alternative is to use Python Playwright which supports many different browsers without any hassle.
Read More:
Good Practices for using Pyppeteer
Here are some of the best practices for expend Pyppeteer:
1. Use waitForSelector ()Instead of Fixed Timeouts
- Avoid await page.waitFor (5000).
- Use await page.waitForSelector (& # 8216; # element & # 8217;)to wait until an element actually look.
2. Run in Headless Mode for Performance
- Use headless mode (headless: true) for fast performance unless debugging.
- When debugging, substitution toheadless: falseand enableslowMo.
3. Close Browser Instances Properly
- Always close the browser usingawait browser.close ()to avoid retentiveness leaks.
- Use try-finally orprocess.on (& # 8216; exit & # 8217;)to ensure cleanup.
4. Isolate Browser Contexts:Use browser.createIncognitoBrowserContext ()for independent session (good for multi-user mechanization or try).
5. Throttle Requests for Scraping:Respect server load; add random delay between requests to mimic human behavior.
6. Intercept and Block Unnecessary Requests:Use page.setRequestInterception (true)to embarrass images, typeface, and ads for faster performance.
7. Use Page Evaluation Wisely:Prefer page. $ eval () and page.evaluate ()for in-page computations rather than sending raw information back to Python.
8. Handle Exceptions Gracefully:Use try-catchblock and meaningful error messages to make debug easier.
9. Leverage Async/Await Correctly:Ensure all async operations are awaited properly to deflect race weather or irregular behaviors.
10. Keep Dependencies Updated:Pyppeteer depends on Chromium and internal protocols—stay updated to avoid break due to browser protocol changes.
How to run Pyppeteer Tests on Real Devices with BrowserStack Automate
Pyppeteer is in the care phase and it is no longer actively maintained. As mentioned before, the latest version of the Pyppeteer doesn & # 8217; t grant to download the latest browsers. Since Pyppeteer stopped fighting development there is not much support for the cloud execution.
Even though this can be reach through some hacks, constancy, and dependableness can not be guaranteed.
Note:However, If you have a cloud execution requirement you can transmigrate your task to Puppeteer and JavaScript. If you feel programming language is a barrier you can consider swap to Playwright Python.
supports both Puppeteer JavaScript and Playwright Python seamlessly without any hassle. You can and screen your covering on 3500+ real device to test on the cloud.
Conclusion
Puppeteer is the most popular instrument for test automation, however, it lacks programming language support early than JavaScript, to whelm this challenge Pyppeteer is a fork from Puppeteer and written wrapper to support Python language.
Due to recent developments, the Pyppeteer project is not actively maintained. When it comes to automation Cross browser examination, Cloud trial execution, and parallel execution are the most crucial thing to attain the ROI. However, Pyppeteer lacks all three all-important feature.
Unless you test your covering on multiple twist and browser combinations you can not predict the behavior of the application in production and it is always riskier. As recommended by the Pyppeteer squad consider switch to Playwright Python which enables all important features without any fuss.
Additionally, supports Playwright seamlessly, this helps to attain good ROI from automation and also help to release your code with confidence.
On This Page
- What is Pyppeteer?
- Use cases of Pyppeteer
- Differences between Puppeteer and Pyppeteer
- How to set up Pyppeteer?
- How to perform various Actions on Pyppeteer
- Managing Cookies with Pyppeteer
- Handling iFrames with Pyppeteer
- Handling Alerts and Pop-ups with Pyppeteer
- Handling Dynamic Content with Pyppeteer
- Parallel Execution with Pyppeteer
- Cross Browser Testing with Pyppeteer
- Better Practices for using Pyppeteer
- How to run Pyppeteer Tests on Existent Devices with BrowserStack Automate
# Ask-and-Contributeabout this subject with our Discord community.
Related Guides
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free