Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang Selenium package - connect to selenium server and headless chrome

I'm using the Go selenium package https://godoc.org/github.com/tebeka/selenium

And I'm running headless chrome + selenium-server inside a docker container on localhost:4444

The server seems to be fine since I can access the web console via http://localhost:4444/wd/hub/static/resource/hub.html

But I'm trying to get the "Hello world" example to work with the existing docker container.

This is the example from the GoDocs page for the selenium driver:

// Run some code on play.golang.org and display the result
package main

import (
    "fmt"
    "time"

    "github.com/tebeka/selenium"
)

var code = `
package main
import "fmt"

func main() {
    fmt.Println("Hello WebDriver!\n")
}
`

// Errors are ignored for brevity.

func main() {
    // Connect to the selenium server
    caps := selenium.Capabilities{"browserName": "firefox"}
    wd, err := selenium.NewRemote(caps, "http://127.0.0.1:4444")
    if err != nil {
        fmt.Println(err)
    }
    defer wd.Quit()

    // Get simple playground interface
    wd.Get("http://play.golang.org/?simple=1")

    // Enter code in textarea
    elem, _ := wd.FindElement(selenium.ByCSSSelector, "#code")
    elem.Clear()
    elem.SendKeys(code)

    // Click the run button
    btn, _ := wd.FindElement(selenium.ByCSSSelector, "#run")
    btn.Click()

    // Get the result
    div, _ := wd.FindElement(selenium.ByCSSSelector, "#output")

    output := ""
    // Wait for run to finish
    for {
        output, _ = div.Text()
        if output != "Waiting for remote server..." {
            break
        }
        time.Sleep(time.Millisecond * 100)
    }

    fmt.Printf("Got: %s\n", output)
}

I tried changing the "browserName" to "chrome" but I get this error:

panic: got content type "text/html", expected "application/json"

goroutine 1 [running]:
main.main()
    /home/user01/Code/golang_src/golang_exercises/33_selenium/selenium.go:28 +0x457
exit status 2

I can't find anything in the GoDoc selenium documentation regarding the chrome browser and how to connect to it via the selenium-server.

I would appreciate any hints as to what might be going wrong here.

Update:

It seems that removing the URL address and leaving it empty has fixed the connection problems:

wd, err := selenium.NewRemote(caps, "")

That said, I'm still having issues with the example. Mainly it seems like it connects to the Go Playground website, gets the right elements, but when it comes to sending the input elem.SendKeys(code) it doesn't send it properly and the text box is empty. Resulting in bad output from the Playground:

Got: can't load package: package main: 
tmp/sandbox573608783/main.go:1:1: expected 'package', found 'EOF'

Program exited.
like image 650
Rtsne42 Avatar asked Oct 28 '25 04:10

Rtsne42


2 Answers

try run it in headless mode:

caps := selenium.Capabilities{"browserName": "chrome"}

chromeCaps := chrome.Capabilities{
    Path:  "",
    Args: []string{
        "--headless", // <<<
        "--no-sandbox",
        "--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7",
    },
}
caps.AddChrome(chromeCaps)

wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://127.0.0.1:%d", port))
like image 186
DiveInto Avatar answered Oct 29 '25 21:10

DiveInto


I also use selenium in Docker, it can run by this:

wd, err := selenium.NewRemote(caps, "http://127.0.0.1:4444/wd/hub")

like image 30
Mayne Wong Avatar answered Oct 29 '25 20:10

Mayne Wong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!