selenium

package module
v1.1.0 Latest Latest
Warning

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

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

README

Go Selenium Library

Go Reference Apache 2.0 License

Overview

This is a Golang Library for Selenium

Support Web Driver

geckodriver Microsoft Edge WebDriver ChromeDriver

The web drivers can be downloaded from:

Browser Driver Download Page
Firefox geckodriver https://github.com/mozilla/geckodriver
Microsoft Edge Microsoft Edge WebDriver Please see the documention of Microsoft Edge WebDriver
Google Chrome ChromeDriver Please see the documention of ChromeDriver

How to use

You should download one of the web drivers before writting the code

⚠️Note: If you use Microsoft Edge WebDriver or ChromeDriver, make sure the drive you downloaded is belone to your browser.

Read More

Documentation

Overview

Example

Example of Mozilla Firefox.

geckodriver can be found in https://github.com/mozilla/geckodriver/releases

// If the path enviroment is not set, gecko driver cannot find the browser automaticly.
//
// In this situation, please use NewWebDriverWithOption, for example:
//
// 	w, err := selenium.NewWebDriverWithOption(
// 		"geckodriver.exe",
// 		&selenium.WebDriverOption{
// 			Browser: "C:/Program Files/Mozilla/firefox.exe"
// 		}
// 	)
//
w, err := selenium.NewWebDriver("geckodriver.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)
}

defer s.Close()

// To navigate to https://example.com
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")
)

The errors of webdriver or session

View Source
var (
	ErrNotInteractableFormControl = errors.New("element is not interactable")
)

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) Attribute added in v1.1.0

func (e *Element) Attribute(attrName string) string

Set the element's attribute value.

func (*Element) Click

func (e *Element) Click()

func (*Element) GetCSSValue

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

func (*Element) GetElementBySelectQuery added in v1.1.0

func (e *Element) GetElementBySelectQuery(query string) (*Element, error)

func (*Element) GetElementByTagName added in v1.1.0

func (e *Element) GetElementByTagName(tagName string) (*Element, error)

func (*Element) GetElementByXPath added in v1.1.0

func (e *Element) GetElementByXPath(xpath string) (*Element, error)

func (*Element) GetElementsBySelectQuery added in v1.1.0

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

func (*Element) GetElementsByTagName added in v1.1.0

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

func (*Element) GetElementsByXPath added in v1.1.0

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

func (*Element) GetRect

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

func (*Element) InnerText

func (e *Element) InnerText() string

func (*Element) Property added in v1.1.0

func (e *Element) Property(propName string) string

Set the element's property value.

func (*Element) Screenshot

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

Take the screenshot of the element e and write to out.

func (*Element) SetValue

func (e *Element) SetValue(value string) error

Set the element's value.

This func only works when the element is interactable, such as an input or a textarea.

If the element is not interactable, it will return ErrNotInteractableFormControl.

func (Element) String

func (e Element) String() string

func (*Element) Visibility

func (e *Element) Visibility() bool

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
Example

This example is to take screenshot of https://example.org

w, err := selenium.NewWebDriver("geckodriver.exe")
if err != nil {
	log.Fatal(err)
}
defer w.Close()

s, err := w.Run()
if err != nil {
	log.Fatal(err)
}
defer s.Close()

s.Navigate("https://example.org")

// Take screenshot with desktop size (1920x1200)
s.SetWindowSize(selenium.WindowSize{
	Width:  1920,
	Height: 1200,
})

desktopScreenshot, err := os.CreateTemp(os.TempDir(), "screenshot-desktop-*.png")
if err != nil {
	log.Fatal(err)
}
defer desktopScreenshot.Close()
s.Screenshot(desktopScreenshot)

// Take screenshot with mobile size (390x844)
s.SetWindowSize(selenium.WindowSize{
	Width:  390,
	Height: 844,
})
mobileScreenshot, err := os.CreateTemp(os.TempDir(), "screenshot-mobile-*.png")
if err != nil {
	log.Fatal(err)
}
defer mobileScreenshot.Close()
s.Screenshot(mobileScreenshot)
Output:

func (*Session) SetWindowSize

func (s *Session) SetWindowSize(val WindowSize)

To update the selenium's window size by val.

The x, y of val should between -2³¹ and (2³¹ - 1); The width and height of val should between 0 and (2³¹ - 1)

func (*Session) SwitchToTab

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

func (*Session) WindowMaximizeSize

func (s *Session) WindowMaximizeSize()

func (*Session) WindowSize

func (s *Session) WindowSize() WindowSize

To get the size of selenium's window.

type WebDriver

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

func NewWebDriver

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

func NewWebDriverWithOption

func NewWebDriverWithOption(driverpath string, opt *WebDriverOption) (*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 WebDriverOption

type WebDriverOption struct {
	Browser string      // The browser binary's path, such as "C:/Program Files/Mozilla Firefox/firefox.exe"
	Logger  *log.Logger // The log of the WebDriver.
}

type WindowSize

type WindowSize struct {
	X      int
	Y      int
	Width  int
	Height int
}

Jump to

Keyboard shortcuts

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