1

chromium not working on google colab

Hi, I want to run puppeteer on google colab.

running test code with !node --trace-warnings test.js says:

Command '/usr/bin/chromium-browser' requires the chromium snap to be installed. Please install it with:snap install chromium

installed chromium with

!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin

!apt install chromium-browser  # also gave same result

puppeteer code

const puppeteer = require('puppeteer');
//varv');
const dotenv = require('dotenv');
dotenv.config();

(async () => {
  const browser = await puppeteer.launch({executablePath: '/usr/bin/chromium-browser'});
  const page = await browser.newPage();

  await page.goto('https://www.google.com');

  await page.screenshot({
    path: 'google.png',
    fullPage: true
  });

  await browser.close();
})();

Tried

  • I tried installing chromium with 'snap install chromium' it says: error: cannot communicate with server: Post http://localhost/v2/snaps/chromium: dial unix /run/snapd.socket: connect: no such file or directory

  • Tried installing snapd

  • !systemctl status snapd.service # snapd: unrecognized service

  • !sudo apt update && upgrade

  • !sudo apt install snapd

  • !which snapd # error: cannot communicate with server: Post http://localhost/v2/snaps/chromium: dial unix /run/snapd.socket: connect: no such file or directory

Tried brave-browser

  • installation code: https://brave.com/linux/
  • executablePath: "/opt/brave.com/brave"
  • error: (node:42514) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process! spawn /opt/brave.com/brave EACCES
  • chmod +x /opt/brave.com/brave # did not solve

Tried google-chrome: Worked while creating this question

references:

https://colab.research.google.com/drive/168X6Zo0Yk2fzEJ7WDfY9Q_0UOEmHSrZc?usp=sharing#scrollTo=_Yf4OfPBAAPR

https://forum.snapcraft.io/t/snap-d-error-cannot-communicate-with-server-connection-refused/6093/23

1 Answers1

-1

To use Chromium with Puppeteer on Google Colab, you can install Puppeteer and launch Chromium using the following code:

!pip install puppeteer

Now python Code.

import asyncio
from pyppeteer import launch

async def main():
 browser = await launch(headless=False, args=['--no-sandbox'])
 page = await browser.newPage()
 await page.goto('https://www.example.com')
 await browser.close()

asyncio.get_event_loop().run_until_complete(main())

Note that the headless option is set to False to launch a visible Chromium browser. The --no-sandbox argument is also added to run Chromium in a containerized environment, which is necessary when running on Google Colab.

With this code, you can launch a Chromium browser, create a new page, navigate to a URL, and close the browser, all using Puppeteer. From here, you can use the Puppeteer API to automate tasks or extract data from web pages.

Christopher Nolan
  • 930
  • 1
  • 11
  • 15