Using Kameleo with Puppeteer framework

  • Created

When you are working with Kameleo and Puppeteer:

  1. You will use Kameleo Local API to manage your virtual browser profiles and launch Kameleo's built-in browsers.
  2. You will use a Puppeteer client library to drive the browser using the Chrome DevTools Protocol.

Automate any browsing activity with Puppeteer. We'll help with keeping your scripts undetected by the sites you're visiting so that they won't see automation tools in use. Let's see how.

Before reading this article, we recommend reading the Getting started with Kameleo Automation article.

Known limitations

  • Puppeteer can be used only in Chromium-based browsers, as Firefox's implementation is not yet complete, and their API is not fully reliable yet. So you will be able to run it with Chroma.
  • Do not use multiple browser contexts when working with Puppeteer, as it can cause unexpected behavior. If you would like to use several contexts, please utilize Kameleo's virtual browser profiles.
  • Do not open any incognito window as Kameleo is not yet fully functional in incognito mode.

Best practices

Do not use any other browser fingerprint modifier tool besides Kameleo (such as puppeteer-extra-plugin-stealth, Canvas Fingerprint Defender, etc.) because those tools may interfere with Kameleo's ability to spoof correctly.

Install Puppeteer

  • To work with Puppeteer, you need to install its official library.

    npm i puppeteer
  • To work with Puppeteer, you need to install PuppeteerSharp, an unofficial .NET port of the Puppeteer Javascript library.

    Install-Package PuppeteerSharp
  • To work with Puppeteer, you need to install Pyppeteer, an unofficial Python port of the Puppeteer Javascript library.

    pip install pyppeteer

Connect Puppeteer to a Kameleo profile

  1. Follow this guide for starting a profile from your script. Once the profile is up and running, you can move on to the next step to control it with Puppeteer commands.
  2. You need to connect to the browser through a WebSocket URL through the Kameleo.CLI component's /puppeteer/{profileId} endpoint.
    • const puppeteer = require("puppeteer");   
      
      const browserWSEndpoint = `ws://localhost:${kameleoPort}/puppeteer/${profile.id}`;
      const browser = await puppeteer.connect({
        browserWSEndpoint,
        defaultViewport: null
      });
      const page = await browser.newPage();
    • using PuppeteerSharp;
      
      var browserWsEndpoint = $"ws://localhost:{KameleoPort}/puppeteer/{profile.Id}";
      var browser = await Puppeteer.ConnectAsync(
        new ConnectOptions {
          BrowserWSEndpoint = browserWsEndpoint,
          DefaultViewport = null
        });
      var page = await browser.NewPageAsync();
    • import pyppeteer
      
      browser_ws_endpoint = f'ws://localhost:{kameleo_port}/puppeteer/{profile.id}'
      browser = await pyppeteer.launcher.connect(
        browserWSEndpoint=browser_ws_endpoint,
        defaultViewport=False)
      page = await browser.newPage()
  3. Then you can use any Puppeteer command to drive the browser, for example:
    • await page.goto('https://google.com');
    • await page.GoToAsync("https://google.com");
    • await page.goto('https://google.com')
  4. Make sure to stop the browser. The virtual browser profile is then stored in you local workspace. Optionally you can export it to a .kameleo file, so you can load it later on another computer as well. Or simply you can delete the profile from Kameleo's workspace once you don't need it anymore. This way, you can keep your resource usage low.
    • await client.stopProfile(profile.id);
      await client.exportProfile(profile.id, { body: { path: `${__dirname}\\test.kameleo` } }); // optional
      await client.deleteProfile(profile.id); // optional
    • await client.StopProfileAsync(profile.Id);
      await client.ExportProfileAsync(profile.Id, new SaveProfileRequest(
        Path.Combine(Environment.CurrentDirectory,"test.kameleo"))); // optional
      await client.DeleteProfileAsync(profile.Id); // optional
      
    • client.stop_profile(profile.id)
      path = f'{os.path.dirname(os.path.realpath(__file__))}\\test.kameleo'
      result = client.export_profile(profile.id, body=SaveProfileRequest(path=path)) # optional
      client.delete_profile(profile.id); # optional

If you want to see the full example code, click any of the links below:

Was this article helpful?

0 out of 1 found this helpful