Those of you who write crawlers know about browser automation, such as Selenium or Puppeteer. I use Selenium a lot. Selenium is a tool for testing Web applications. Selenium tests run directly in the browser, just as real users do. So we’ll use Selenium to mimic user operations and browser crawl data.

We used Python as the development language, so let’s try Go Selenium today.

The installation

One of the dependencies I’m currently using is github.com/tebeka/sele… , the functions are complete and under maintenance.

go get -t -d github.com/tebeka/selenium
Copy the code

In addition, we need to install WebDriver for different types of browsers, Google Chrome needs to install ChromeDriver, Firefox needs to install Geckodriver.

case

Here we are using Google Chrome. First we need to specify the location of the ChromeDriver and launch a WebDriver Server, and then we are ready to operate the browser.

package main

import (
    "fmt"
    "os"
    "strings"
    "time"

    "github.com/tebeka/selenium"
)

const (
    chromeDriverPath = "/path/to/chromedriver"
    port             = 8080
)

func main(a) {
    // Start a WebDriver server instance
    opts := []selenium.ServiceOption{
        selenium.Output(os.Stderr),            // Output debug information to STDERR.
    }
    selenium.SetDebug(true)
    service, err := selenium.NewChromeDriverService(chromeDriverPath, port, opts...)
    iferr ! =nil {
        panic(err) // panic is used only as an example and is not otherwise recommended.
    }
    defer service.Stop()

    // Connect to the WebDriver instance running locally.
    caps := selenium.Capabilities{"browserName": "chrome"}
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
    iferr ! =nil {
        panic(err)
    }
    defer wd.Quit()

    // Navigate to the simple playground interface.
    if err := wd.Get("http://play.golang.org/?simple=1"); err ! =nil {
        panic(err)
    }

    // Get a reference to the text box containing code.
    elem, err := wd.FindElement(selenium.ByCSSSelector, "#code")
    iferr ! =nil {
        panic(err)
    }
    // Remove the boilerplate code already in the text box.
    iferr := elem.Clear(); err ! =nil {
        panic(err)
    }

    // Enter some new code in text box.
    err = elem.SendKeys(` package main import "fmt" func main() { fmt.Println("Hello WebDriver!" )} `)
    iferr ! =nil {
        panic(err)
    }

    // Click the run button.
    btn, err := wd.FindElement(selenium.ByCSSSelector, "#run")
    iferr ! =nil {
        panic(err)
    }
    iferr := btn.Click(); err ! =nil {
        panic(err)
    }

    // Wait for the program to finish running and get the output.
    outputDiv, err := wd.FindElement(selenium.ByCSSSelector, "#output")
    iferr ! =nil {
        panic(err)
    }

    var output string
    for {
        output, err = outputDiv.Text()
        iferr ! =nil {
            panic(err)
        }
        ifoutput ! ="Waiting for remote server..." {
            break
        }
        time.Sleep(time.Millisecond * 100)
    }

    fmt.Printf("%s", strings.Replace(output, "\n\n"."\n".- 1))

    // Example Output:
    // Hello WebDriver!
    //
    // Program exited.
}
Copy the code

conclusion

It’s not very complicated to use, but it feels like Go Selenium is not very popular, github.com/tebeka/sele… The number of stars on GitHub is only 1K +.