selenium icon indicating copy to clipboard operation
selenium copied to clipboard

More examples

Open arvindchandra opened this issue 8 years ago • 5 comments

I wanted to get started with chrome testing using client library. But not able to find relevant sample code. If there was a sample code how to start browser and go to URL(eg google.com) and close the browser that will be great help for people who are getting started.

Eg :+1:

I used

  service, err :=
    selenium.NewChromeDriverService("/home/johndoe/workspace/toolchain/x86_64-linux/chromedriver",
    4444)
  caps := selenium.Capabilities{"browserName": "chrome"}
  wd, err := selenium.NewRemote(caps, "https://www.google.com")
  wd.Refresh()                    
  if err != nil {
    glog.Errorf("Failed")
    panic(err) // panic is used only as an example and is not otherwise recommended.
  }
  defer service.Stop()

And the above code did not start browser but did not fail. If there was a sample code I could have just coded based on that.

arvindchandra avatar Nov 13 '17 17:11 arvindchandra

Thanks for the feedback and suggestions.

Did you see the example code in the documentation?

https://godoc.org/github.com/tebeka/selenium

(Click "Example" to expand it. It doesn't seem to have a direct link).

Also, in general, a good way to understand how to use any API is by looking at its unit tests:

https://github.com/tebeka/selenium/blob/master/remote_test.go

I'll definitely add more examples to the documentation, since we only have the one and it doesn't exercise a huge swath of the API.

minusnine avatar Nov 16 '17 15:11 minusnine

Unit tests are too complex to write a simple program that will open a browser. Here is a sample if anybody gets stuck

package main

import (
	"fmt"
	"github.com/tebeka/selenium"
	"net"
	"os"
	"os/exec"
	"strconv"
)

func main() {

	browserPath := GetBrowserPath("chromium")
	port, err := pickUnusedPort()

	var opts []selenium.ServiceOption
	service, err := selenium.NewChromeDriverService("chromedriver",
		port, opts...)

	if err != nil {
		fmt.Printf("Error starting the ChromeDriver server: %v", err)
	}

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

	wd, err := selenium.NewRemote(caps, "http://127.0.0.1:"+strconv.Itoa(port)+"/wd/hub")
	if err != nil {
		panic(err)
	}

	wd.Refresh()

	wd.Get("https://google.com")
	defer service.Stop()
}

func pickUnusedPort() (int, error) {
	addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
	if err != nil {
		return 0, err
	}

	l, err := net.ListenTCP("tcp", addr)
	if err != nil {
		return 0, err
	}
	port := l.Addr().(*net.TCPAddr).Port
	if err := l.Close(); err != nil {
		return 0, err
	}
	return port, nil
}

func GetBrowserPath(browser string) string {
	if _, err := os.Stat(browser); err != nil {
		path, err := exec.LookPath(browser)
		if err != nil {
			panic("Browser binary path not found")
		}
		return path
	}
	return browser
}

Download chromedriver and put it in same directory It will open chromium and go to google.com then close

arpitjindal97 avatar May 22 '18 06:05 arpitjindal97

Dear @arpitjindal97 , Thanks for this life / time saving snipet . It worked for me . Also "browserPath" is unused in this code, and It doesn t look for chromedriver in the current Bin directory. It looks in $PATH on my Ubuntu 18.04. I suppose its the missing part...passing "browserPath" to the webdriver instentiation...

Have a nice day For google to crawl : how to make tebeka selenium go webdriver work with chrome web browser

hubyhuby avatar Sep 16 '18 11:09 hubyhuby

On macOS you can:

brew cask install chromedriver

prologic avatar Aug 06 '20 04:08 prologic

For anyone wondering, here's how you use the browserPath and start chrome/chromium headless:

package main

import (
	"fmt"
	"github.com/tebeka/selenium"
	"github.com/tebeka/selenium/chrome"
	"net"
	"os"
	"os/exec"
	"strconv"
)

const (
    proxyPort = 1337
)

func main() {
	browserPath := GetBrowserPath("chromium")
	port, err := pickUnusedPort()

	var opts []selenium.ServiceOption
	service, err := selenium.NewChromeDriverService("chromedriver",
		port, opts...)

	if err != nil {
		fmt.Printf("Error starting the ChromeDriver server: %v", err)
	}

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

        // NB: Path is the important part here
	caps.AddChrome(
		chrome.Capabilities{
			Path: browserPath,
			Args: []string{
				"--window-size=1920,1080",
				"--headless",
				"--no-sandbox",
			},
		},
	)

	wd, err := selenium.NewRemote(caps, "http://127.0.0.1:"+strconv.Itoa(port)+"/wd/hub")
	if err != nil {
		panic(err)
	}

	wd.Refresh()

	wd.Get("https://google.com")
	defer service.Stop()
}

func pickUnusedPort() (int, error) {
	addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
	if err != nil {
		return 0, err
	}

	l, err := net.ListenTCP("tcp", addr)
	if err != nil {
		return 0, err
	}
	port := l.Addr().(*net.TCPAddr).Port
	if err := l.Close(); err != nil {
		return 0, err
	}
	return port, nil
}

func GetBrowserPath(browser string) string {
	if _, err := os.Stat(browser); err != nil {
		path, err := exec.LookPath(browser)
		if err != nil {
			panic("Browser binary path not found")
		}
		return path
	}
	return browser
}

ChristianSch avatar Mar 17 '22 09:03 ChristianSch