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 ¶
- Variables
- type Element
- func (e *Element) Attribute(attrName string) string
- func (e *Element) Click()
- func (e *Element) GetCSSValue(styleName string) string
- func (e *Element) GetElementBySelectQuery(query string) (*Element, error)
- func (e *Element) GetElementByTagName(tagName string) (*Element, error)
- func (e *Element) GetElementByXPath(xpath string) (*Element, error)
- func (e *Element) GetElementsBySelectQuery(query string) ([]*Element, error)
- func (e *Element) GetElementsByTagName(tagName string) ([]*Element, error)
- func (e *Element) GetElementsByXPath(xpath string) ([]*Element, error)
- func (e *Element) GetRect() (result image.Rectangle)
- func (e *Element) InnerText() string
- func (e *Element) Property(propName string) string
- func (e *Element) Screenshot(out io.Writer) error
- func (e *Element) SetValue(value string) error
- func (e Element) String() string
- func (e *Element) Visibility() bool
- type Session
- func (s *Session) AddTab()
- func (s *Session) Close() error
- func (s *Session) CurrentPageTitle() string
- func (s *Session) CurrentURL() string
- func (s *Session) GetElementBySelectQuery(query string) (*Element, error)
- func (s *Session) GetElementByTagName(tagName string) (*Element, error)
- func (s *Session) GetElementByXPath(xpath string) (*Element, error)
- func (s *Session) GetElementsBySelectQuery(query string) ([]*Element, error)
- func (s *Session) GetElementsByTagName(tagName string) ([]*Element, error)
- func (s *Session) GetElementsByXPath(xpath string) ([]*Element, error)
- func (s *Session) Navigate(urlPath string) error
- func (s *Session) Screenshot(out io.Writer) error
- func (s *Session) SetWindowSize(val WindowSize)
- func (s *Session) SwitchToTab(tabIndex int) error
- func (s *Session) WindowMaximizeSize()
- func (s *Session) WindowSize() WindowSize
- type WebDriver
- type WebDriverOption
- type WindowSize
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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
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) GetCSSValue ¶
func (*Element) GetElementBySelectQuery ¶ added in v1.1.0
func (*Element) GetElementByTagName ¶ added in v1.1.0
func (*Element) GetElementByXPath ¶ added in v1.1.0
func (*Element) GetElementsBySelectQuery ¶ added in v1.1.0
func (*Element) GetElementsByTagName ¶ added in v1.1.0
func (*Element) GetElementsByXPath ¶ added in v1.1.0
func (*Element) Screenshot ¶
Take the screenshot of the element e and write to out.
func (*Element) SetValue ¶
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) Visibility ¶
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
func (*Session) Close ¶
Close the session
If it is already closed, the func will return ErrSessionClosed.
func (*Session) CurrentPageTitle ¶
func (*Session) CurrentURL ¶
Get current URL of the session.
If it has any error, this function will return "about:blank"
func (*Session) GetElementBySelectQuery ¶
To get the 1st element within the document that matches the query.
This is similar as document.querySelector() method in Javascript.
func (*Session) GetElementByTagName ¶
To get the 1st element within the document that matches the given tag name.
func (*Session) GetElementByXPath ¶
To get the 1st element within the document that matches the given XPath.
func (*Session) GetElementsBySelectQuery ¶
To get the elements within the document that matches the query.
This is similar as document.querySelectorAll() method in Javascript.
func (*Session) GetElementsByTagName ¶
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 ¶
To get the elements within the document that matches the given XPath.
func (*Session) Screenshot ¶
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 (*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 NewWebDriverWithOption ¶
func NewWebDriverWithOption(driverpath string, opt *WebDriverOption) (*WebDriver, error)