selenium

package module
v0.0.0-...-9fb806a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

README

Go Selenium Library

Go Reference Apache 2.0 License

A Golang Library for Selenium

Documentation

Overview

Example
// Example of Mozilla Firefox.
//
// geckodriver can be found in https://github.com/mozilla/geckodriver/releases
w, err := selenium.NewWebDriver("geckodriver.exe")

// If the path is not set, gecko driver cannot find the browser automaticly.
//
// In this situation, please use NewWebDriverWithBrowserBinary, for example:
// 	w, err := selenium.NewWebDriver("geckodriver.exe", "C:/Program Files/Mozilla/firefox.exe")
//

if err != nil {
	log.Print("Fail of gecko driver")
	log.Fatal(err)
	return
}

defer w.Close()

// To run the driver and to create a new session
s, err := w.Run()
if err != nil {
	log.Fatal(err)
}

s.Navigate("https://example.com")
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrCannotCreateSession = errors.New("cannot create selenium session")
	ErrNoSessionID         = errors.New("no session id")
	ErrUnknownWebdriver    = errors.New("unknown webdriver")
	ErrSessionClosed       = errors.New("session closed")
)

Functions

This section is empty.

Types

type Element

type Element struct {
	TagName string
	ID      string // element ID attribute
	Class   []string
	Role    string
	Name    string //element name attribute
	Type    string //element name attribute
	// contains filtered or unexported fields
}

HTML element

func (*Element) Click

func (e *Element) Click()

func (*Element) GetCSSValue

func (e *Element) GetCSSValue(styleName string) string

func (*Element) GetRect

func (e *Element) GetRect() (result image.Rectangle)

func (*Element) InnerText

func (e *Element) InnerText() string

func (*Element) Screenshot

func (e *Element) Screenshot(out io.Writer) error

func (Element) String

func (e Element) String() string

type Session

type Session struct {
	// contains filtered or unexported fields
}

func (*Session) AddTab

func (s *Session) AddTab()

func (*Session) Close

func (s *Session) Close() error

Close the session

If it is already closed, the func will return ErrSessionClosed.

func (*Session) CurrentPageTitle

func (s *Session) CurrentPageTitle() string

func (*Session) CurrentURL

func (s *Session) CurrentURL() string

Get current URL of the session.

If it has any error, this function will return "about:blank"

func (*Session) GetElementBySelectQuery

func (s *Session) GetElementBySelectQuery(query string) (*Element, error)

To get the 1st element within the document that matches the query.

This is similar as document.querySelector() method in Javascript.

func (*Session) GetElementByTagName

func (s *Session) GetElementByTagName(tagName string) (*Element, error)

To get the 1st element within the document that matches the given tag name.

func (*Session) GetElementByXPath

func (s *Session) GetElementByXPath(xpath string) (*Element, error)

To get the 1st element within the document that matches the given XPath.

func (*Session) GetElementsBySelectQuery

func (s *Session) GetElementsBySelectQuery(query string) ([]*Element, error)

To get the elements within the document that matches the query.

This is similar as document.querySelectorAll() method in Javascript.

func (*Session) GetElementsByTagName

func (s *Session) GetElementsByTagName(tagName string) ([]*Element, error)

To get the elements within the document that matches the the given tag name.

This is similar as document.getElementsByTagName() method or document.getElementsByTagNameNS() in Javascript.

func (*Session) GetElementsByXPath

func (s *Session) GetElementsByXPath(xpath string) ([]*Element, error)

To get the elements within the document that matches the given XPath.

func (*Session) Navigate

func (s *Session) Navigate(urlPath string) error

func (*Session) Screenshot

func (s *Session) Screenshot(out io.Writer) error

func (*Session) SwitchToTab

func (s *Session) SwitchToTab(tabIndex int) error

func (*Session) URL

func (s *Session) URL() *url.URL

type WebDriver

type WebDriver struct {
	Sessions []*Session
	// contains filtered or unexported fields
}

func NewWebDriver

func NewWebDriver(driverpath string) (*WebDriver, error)

func NewWebDriverWithBrowserBinary

func NewWebDriverWithBrowserBinary(driverpath string, browserBinaryPath string) (*WebDriver, error)

func (*WebDriver) AddSession

func (w *WebDriver) AddSession() (*Session, error)

func (*WebDriver) Close

func (w *WebDriver) Close() error

func (*WebDriver) Run

func (w *WebDriver) Run() (*Session, error)

type WebDriverBody

type WebDriverBody struct {
	Value map[string]any `json:"value,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL