Bacancy Bacancy
      • About Company
      • Resources

      About Company

      About Us Leadership Team Customer Reviews Awards & Recognition
      Infrastructure Our Locations Partnership

      Resources

      Press Room Blog Insights
      We are great place to work certified™

      Building and Sustaining High-Trust, High-Performance Culture

      Get Quote
    • Engagement Models

      Hiring Software Developers becomes easier with just a few clicks.

      Software Development Outsourcing

      End-to-end delivery of custom solutions aligned to your roadmap.

      Staff Augmentation

      Scale your in-house team with pre-vetted specialists on demand.

      Dedicated Teams

      Get dedicated engineers who work exclusively on your project.

      • Enterprise Services
      • IT Services
      • Data Analytics
      • Cloud Services
      • AI & ML
      • Platforms

      Enterprise Services

      Digital Transformation Business Process Automation Digital Product Engineering Enterprise App Development Custom Software Development

      IT Services

      Legacy App Modernization DevOps & SRE Full Stack Development AI Testing & QA Automation

      Data Analytics

      Data Visualization & Reporting Data Engineering & Pipelines Data Science & Predictive Analytics Business Intelligence

      Cloud Services

      Cloud Strategy & Consulting Cloud Migration & Modernization Multi Cloud Management

      AI & ML

      AI Development Agentic AI Generative AI Computer Vision Machine Learning & MLOps

      Platforms

      Salesforce SAP ServiceNow Microsoft Dynamics Snowflake
      High-quality, Cost-effective IT Outsourcing

      Schedule a free discovery session to explore your needs and find tailored solutions with no obligation.

      explore all services
    • Industries
      Healthcare Fintech Real Estate
      Logistics Education Retail & Ecommerce
      Let's Grow Together! Get Quote
      • Front End
      • Backend
      • Mobile
      • Databases
      • DevOps & Infra
      • AI & Data Stack
      • Vibe Coding

      Front End

      React.js Next.js Angular Vue.js TypeScript
      Your Very Own UI/UX Architects

      Experience smooth navigation and user-friendly designs with our front-end expertise.

      Hire Frontend Developer

      Backend

      Node.js Python Java Spring Boot Laravel .NET C# Golang FastAPI
      Server Solutions To Change Power Dynamics

      Transform your data into digital experiences with optimized coding standards.

      Hire Backend Developer

      Mobile

      iOS Android Flutter React Native
      Innovating Mobile-Friendly App Solutions

      Create dynamic mobile apps that make your brand stand out from the crowd.

      Hire Mobile App Developer

      Databases

      PostgreSQL MongoDB MySQL Redis Supabase
      Dedicated Talent With Skilled Approach

      Bring your digital visions to life with a hired resource at your convenience.

      Hire Dedicated Developer

      DevOps & Infra

      AWS Azure Google Cloud Docker Kubernetes Terraform
      Redefining Scalable Digital Infrastructures

      Make your data accessible worldwide at will, and leave the stress behind.

      Get Quote

      AI & Data Stack

      OpenAI LangChain LlamaIndex Apache Spark Airflow Tableau PowerBI Databricks
      Guiding Decisions With Data-Driven Insights

      Transition from your gut calls to actionable insights with our rich Data Science expertise.

      Get Quote

      Vibe Coding

      Base44 Claude Code Cursor Lovable Github Copilot
      Your AI-Native Development Team

      Skip the boilerplate. Our vibe coding experts use AI-first tools to go from prompt to product, fast.

      Hire Vibe Coding Developer
  • Case Studies
  • Contact Us
Find a Developer book a 30 min call
      • About Us
      • Leadership Team
      • Customer Reviews
      • Awards & Recognition
      • Infrastructure
      • Our Locations
      • Partnership
      • Press Room
      • Blog
      • Insights
      • Digital Transformation
      • Business Process Automation
      • Digital Product Engineering
      • Enterprise App Development
      • Custom Software Development
      • Legacy App Modernization
      • DevOps & SRE
      • Full Stack Development
      • AI Testing & QA Automation
      • Data Visualization & Reporting
      • Data Engineering & Pipelines
      • Data Science & Predictive Analytics
      • Business Intelligence
      • Cloud Strategy & Consulting
      • Cloud Migration & Modernization
      • Multi Cloud Management
      • AI Development
      • Agentic AI
      • Generative AI
      • Computer Vision
      • Machine Learning & MLOps
      • Salesforce
      • SAP
      • ServiceNow
      • Microsoft Dynamics
      • Snowflake
    • Healthcare
    • Fintech
    • Real Estate
    • Logistics
    • Education
    • Retail & Ecommerce
      • React.js
      • Next.js
      • Angular
      • Vue.js
      • TypeScript
      • Hire Frontend Developer
      • Node.js
      • Python
      • Java
      • Spring Boot
      • Laravel
      • .NET
      • C#
      • Golang
      • FastAPI
      • Hire Backend Developer
      • iOS
      • Android
      • Flutter
      • React Native
      • Hire Mobile App Developer
      • PostgreSQL
      • MongoDB
      • MySQL
      • Redis
      • Supabase
      • Hire Dedicated Developer
      • AWS
      • Azure
      • Google Cloud
      • Docker
      • Kubernetes
      • Terraform
      • Get Quote
      • OpenAI
      • LangChain
      • LlamaIndex
      • Apache Spark
      • Airflow
      • Tableau
      • PowerBI
      • Databricks
      • Get Quote
      • Base44
      • Claude Code
      • Cursor
      • Lovable
      • Github Copilot
      • Hire Vibe Coding Developer
  • Case Studies
  • Contact Us
  • Find a Developer
  • book a 30 min call
Use Headless Chrome in Golang with Godet

How to Use Headless Chrome in Golang with Godet

Ashvin Kumbhani
Ashvin Kumbhani Director of Engineering
Last Updated on March 10, 2025 | Written By: Ashvin Kumbhani

So many times it happens when we have to test a few cases at the time of development to ensure it is the same as provided on the website. It also happens when we have to fill up the lengthy, tedious, and monotonous form. I encountered this problem while filling up the contact us form at the time of enquiring about a car.

They ask too many questions which the customer had already filled up previously in the application for other dealers. If you have the same data in the database, it’s so easy and convenient to fill the form as I had that same data in my database. I was able to make the form fill up faster by opening up a golang headless chrome instance in my server. And then, I used Golang and godet to access the website, fill-up the way and get the contract printed as a pdf on my server saving the user an ample amount of time with the tedious process of filling up the form. So let me tell you how to do it.

The Procedure

Pre-installed:

  • Golang
  • chromium-browser

We will be going to navigate from Google.com to search some text and then screenshot and print the very first link of the search.

First, let us start by creating a primary go code that just prints out, “Hey All.”

So create a file that satisfies you GOPATH.

FileName: main.go

package main
 
import (
    "log"
)
 
func main() {
    log.Println("Hey All!")
}

Now let us open up chromium-browser by code and kill the process after waiting 2 minutes.

package main
 
import (
    "log"
    "os/exec"
    "time"
)
 
func main() {
    chromeapp := "chromium-browser"
    chromeappArg := []string{"--headless", "--hide-scrollbars", "--remote-debugging-port=9222", "--disable-gpu", "--allow-insecure-localhost"}
    cmd := exec.Command(chromeapp, chromeappArg...)
    err := cmd.Start()
    if err != nil {
        log.Println("cannot start browser", err)
    }
 
    time.Sleep(2 * time.Minute)
 
    killapp := "kill"
    killappArg := []string{"$(lsof -t -i:9222)"}
    cmd = exec.Command(killapp, killappArg...)
    err = cmd.Start()
    if err != nil {
        log.Println("cannot kill processes", err)
    }
}

Now in the 30-second sleep, if you open up another terminal and run lsof -i:9222, this will show you that the chromium-browser has now started and the dev-tools are running on port 9222.

 running on port 9222.

You can see the tabs of the chromium-browser we created on localhost:9222

chromium-browser

If you click on the about: blank, you’ll be able to see the process that we do on the opened tab in our chromium-browser.

Before connecting to chromium, we will need to install godet.

So on your terminal run, go get “github.com/raff/godet”.

Do you need assistance to solve your Golang error?
Hire Golang developer from us to fix the bugs and fine-tune your Golang app user experience.

This package creates a small layer between the google chrome dev tools and Golang.

Now we will navigate to www.google.com to do so add the following code.

package main
 
import (
    "log"
    "os/exec"
    "time"
 
    "github.com/raff/godet"
)
 
func main() {
    chromeapp := "chromium-browser"
    chromeappArg := []string{"--headless", "--hide-scrollbars", "--remote-debugging-port=9222", "--disable-gpu", "--allow-insecure-localhost"}
    cmd := exec.Command(chromeapp, chromeappArg...)
    err := cmd.Start()
    if err != nil {
        log.Println("cannot start browser", err)
    }
 
    // Will wait for chromium to start
    time.Sleep(5 * time.Second)
 
    // connect to Chromium instance
    remote, err := godet.Connect("localhost:9222", true)
    if err != nil {
        log.Println("cannot connect to Chrome instance:", err)
        return
    }
 
    // disconnect when done
    defer remote.Close()
 
    remote.PageEvents(true)
    remote.DOMEvents(true)
 
    _, err = remote.Navigate("https://www.google.com")
    if err != nil {
        log.Println("cannot connect to Chrome instance:", err)
        return
    }
 
    time.Sleep(30 * time.Second)
 
    killapp := "kill"
    killappArg := []string{"$(lsof -t -i:9222)"}
    cmd = exec.Command(killapp, killappArg...)
    err = cmd.Start()
 
    if err != nil {
        log.Println("cannot kill processes", err)
    }
}

The remote, err:= godet.Connect(“localhost:9222”, true) will connect to the chromium-browser and the line defer remote.Close() will close the connection on the full execution of our code. This is preferable to do so that we can prevent memory leaks.

The remote.PageEvents(true) remote.DOMEvents(true) will log all the PageEvents and DomEvents on our terminal.

_, err = remote.Navigate(“https://www.google.com“) this is the magic code that navigates the tab open in chromium-tab to www.google.com.

google

Now we are going to use the EvaluateWrap function to inject JS in the browser.

For more such functions, you can refer to https://github.com/raff/godet.

We will count the times that the page stops loading and inject js based on it.

package main
 
import (
    "log"
    "os/exec"
    "time"
 
    "github.com/raff/godet"
)
 
func main() {
    chromeapp := "chromium-browser"
    chromeappArg := []string{"--headless", "--hide-scrollbars", "--remote-debugging-port=9222", "--disable-gpu", "--allow-insecure-localhost"}
    cmd := exec.Command(chromeapp, chromeappArg...)
    err := cmd.Start()
    if err != nil {
        log.Println("cannot start browser", err)
    }
 
    // Will wait for chromium to start
    time.Sleep(5 * time.Second)
 
    // connect to Chromium instance
    remote, err := godet.Connect("localhost:9222", true)
    if err != nil {
        log.Println("cannot connect to Chrome instance:", err)
        return
    }
 
    // disconnect when done
    defer remote.Close()
 
    remote.PageEvents(true)
    remote.DOMEvents(true)
 
    _, err = remote.Navigate("https://www.google.com")
    if err != nil {
        log.Println("cannot connect to Chrome instance:", err)
        return
    }
 
    count := 0
 
    remote.CallbackEvent("Page.frameStoppedLoading", func(params godet.Params) {
        count = count + 1
        log.Println("TCL: remote.CallbackEvent -> count", count)
        switch count {
        case 1:
            _, err = remote.EvaluateWrap(`
                document.getElementsByClassName('gLFyf gsfi')[0].value = "Bacancy Technology"
                document.getElementsByClassName('gNO89b')[1].click()
            `)
            if err != nil {
                log.Println("Error executing js block 1:", err)
                return
            }
            time.Sleep(5 * time.Second)
        case 2:
            _, err = remote.EvaluateWrap(`
                document.getElementsByClassName('LC20lb')[0].click()
            `)
            if err != nil {
                log.Println("Error executing js block 1:", err)
                return
            }
            time.Sleep(5 * time.Second)
            // take a screenshot
            _ = remote.SaveScreenshot("SearchResultScreenshot.png", 0644, 0, true)
                // print a pdf
                err = remote.SavePDF(`./SearchResultPdf.pdf`, 0644)
                if err != nil {
                    log.Println("Error saving pdf.")
                }
            }
    })

    time.Sleep(30 * time.Second)
 
    killapp := "kill"
    killappArg := []string{"$(lsof -t -i:9222)"}
    cmd = exec.Command(killapp, killappArg...)
    err = cmd.Start()
    if err != nil {
        log.Println("cannot kill processes", err)
    }
}

Explaining what we have done based on cases

Case 1: Fill up the Google search box with “Bacancy Technology and clicking the search button.
Case 2: Clicking the first link and screenshotting and printing the link opened

When you run go run main.go after navigating to the file in your terminal, after 30 seconds, you’ll be able to see 2 files generated in the same directory.

I’ll attach the screenshot for the look of the browser(the one in localhost:9222), the screenshot created, and the pdf generated.

Browser:

Browser

Screenshot:

pdf generated


Expand Your Digital Horizons With Us.

Start a new project or take an existing one to the next level. Get in touch to start small, scale-up, and go Agile.


Or
E-mail us : solutions@bacancy.com

Your Success Is Guaranteed !

Related Articles

Ashvin Kumbhani

June 5, 2026

Golang

Golang for Healthcare Application Development: Secure, Fast, HIPAA-Ready

By : Ashvin Kumbhani

Read More
Ashvin Kumbhani

April 22, 2026

Golang

Golang Cloud Native Development: Cost, Performance, Kubernetes & Scalability

By : Ashvin Kumbhani

Read More
Ashvin Kumbhani

February 18, 2026

Golang

How Much Does Golang App Development Cost in 2026?

By : Ashvin Kumbhani

Read More

Offices and Development Centers

Bacancy Ahmedabad Ahmedabad

15-16, Times Corporate Park, Thaltej, Ahmedabad, 380059

Bacancy Gandhinagar Gandhinagar

422-A, 4th Floor, Pragya Tower Road 11, Block 15, Zone 1, SEZ-PA Gandhinagar, 382355

Bacancy Hyderabad Hyderabad

Awfis, Level 1, N Heights, Plot No 38, Phase 2, Hitech City Hyderabad, 500081

Bacancy Mumbai Mumbai

18th Floor, Cyberone, opp. CIDCO Exhibition Centre, Sector 30, Vashi, Navi Mumbai, 400703

Bacancy Pune Pune

2nd FloorMarisoft-1, Marigold IT Park, Pune - 411014

Bacancy Bengaluru Bengaluru

Raheja Towers, 26/27, Mahatma Gandhi Rd, East Wing, Craig Park Layout, Ashok Nagar, Bengaluru, 560001

Global Presence

Bacancy New Jersey New Jersey

33 South Wood Ave, Suite 600, Iselin NJ 08830

Bacancy California California

535 Mission St 14th floor, San Francisco, CA 94105

Bacancy Massachusetts Massachusetts

501 Boylston St, Boston, MA 02116

Bacancy Florida Florida

4995 NW, 72nd Avenue, Suite 307, Miami, FL, 33166

Bacancy London London

90 York Wy, London N1 9AG, United Kingdom

Bacancy Ontario Ontario

71 Dawes Road, Brampton, On L6X 5N9, Toronto

Bacancy Australia Australia

351A Hampstead Rd, Northfield SA 5085

Bacancy UAE UAE

One Central 8th and 9th Floor - Trade Centre - Trade Centre 2 - Dubai - United Arab Emirates

Bacancy Sweden Sweden

Junkergatan 4, 126 53 Hagersten

Get in Touch

Great Place to Work

Get in Touch

cal-icon

Looking for expert advice?

Schedule a Expert Call


  • Brochure
  • Quality Assurance
  • Resources
  • Tutorials
  • Customer Reviews
  • Privacy Policy
  • FAQs
  • Press Room
  • Contact Us
  • Sitemap
  • Employee

bacancy google review 4.6
bacancy google review
bacancy clutch review 4.8
bacancy clutch review
bacancy glassdoor review 4.5
bacancy glassdoor review
bacancy goodfirms review 4.8
bacancy goodfirms review
iso
  • Bacancy Behance
  • Bacancy Pinterest

Copyright © 2026 BACANCY SERVICES PRIVATE LIMITED All rights reserved.