cr

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2023 License: BSD-2-Clause Imports: 12 Imported by: 7

README

cr

Simple wrapper for chromedp.

Licence: BSD.

Example usage:

package main

import (
    "log"
    "time"

    "github.com/admpub/cr"
)

func main() {

    browser, err := cr.New()
    if err != nil {
        log.Fatalf("Unable to create browser: %s\n", err)
    }
    defer browser.Close()

    if err := browser.Navigate("https://google.com"); err != nil {
        log.Fatalf("Couldn't navigate to page: %s\n", err)
    }

    if err := browser.SendKeys("//input[@id='lst-ib']", "The Big Lebowski"); err != nil {
        log.Fatalf("Couldn't fill out form: %s\n", err)
    }

    if err = browser.Click("//input[@name='btnK']"); err != nil {
        log.Fatalf("Couldn't find the submit button: %s\n", err)
    }
    time.Sleep(time.Second * 15)

}

Why XPath?

XPath seems to be the easiest way to find DOM elements. There is great documentation online, such as the w3schools tutorial, and Google searches for specific things tend to turn up good results.

It is also easy to find an XPath in Chrome by right-clicking on an element and selecting "Inspect."

Clicking on any element in the inspector will show three dots (...) that you can click on and select "Copy XPath" from the "Copy" menu.

Typing CTRL+f in the inspector will provide a text box which can be used to test XPath values before running your code.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("element not found")

ErrNotFound is returned when an XPATH is provided for a DOM element, but it can not be located.

Functions

This section is empty.

Types

type Browser

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

Browser represents a Chrome browser controlled by chromedp.

func New

New instantiates a new Chrome browser and returns a *Browser used to control it.

func (*Browser) Click

func (b *Browser) Click(xpath string) error

Click performs a mouse click on a DOM element.

func (*Browser) ClickByXY

func (b *Browser) ClickByXY(xpath string) error

ClickByXY clicks the browser window in a specific location.

func (*Browser) Close

func (b *Browser) Close() error

Close cleans up the *Browser; this should be called on every *Browser once its work is complete.

func (*Browser) ElementScreenshot

func (b *Browser) ElementScreenshot(urlStr string, selectionElem string, by ...func(s *chromedp.Selector)) ([]byte, error)

func (*Browser) FindElement

func (b *Browser) FindElement(xpath string) error

FindElement attempts to locate a DOM element.

func (*Browser) GetAttributes

func (b *Browser) GetAttributes(xpath string) (map[string]string, error)

GetAttributes returns the HTML attributes of a DOM element.

func (*Browser) GetNodes

func (b *Browser) GetNodes(xpath string) ([]*cdp.Node, error)

GetNodes returns a slice of *chromedp.Node from the chromedp package.

func (*Browser) GetSource

func (b *Browser) GetSource() (string, error)

GetSource returns the HTML source from the browser tab.

func (*Browser) GetTopLeft

func (b *Browser) GetTopLeft(xpath string) (float64, float64, error)

GetTopLeft returns the x, y coordinates of a DOM element.

func (*Browser) Location

func (b *Browser) Location(otherActions ...chromedp.Action) (string, error)

Location returns the current URL.

func (*Browser) MustClick

func (b *Browser) MustClick(xpath string)

MustClick performs a mouse click or ends the program.

func (*Browser) MustNavigate

func (b *Browser) MustNavigate(url string, otherActions ...chromedp.Action)

MustNavigate calls Navigate and ends execution on error.

func (*Browser) MustSendKeys

func (b *Browser) MustSendKeys(xpath, value string)

MustSendKeys sends keystrokes to a DOM element or halts execution.

func (*Browser) Navigate

func (b *Browser) Navigate(url string, otherActions ...chromedp.Action) error

Navigate sends the browser to a URL.

func (*Browser) RunAction

func (b *Browser) RunAction(action chromedp.Action) error

RunAction run single action

func (*Browser) RunTaskWithOther

func (b *Browser) RunTaskWithOther(action chromedp.Action, otherActions ...chromedp.Action) error

RunTaskWithOther run mutiple action

func (*Browser) RunTasks

func (b *Browser) RunTasks(actions ...chromedp.Action) error

RunTasks run mutiple action

func (*Browser) Screenshot

func (b *Browser) Screenshot(urlStr string, quality int64) ([]byte, error)

func (*Browser) SendKeys

func (b *Browser) SendKeys(xpath, value string) error

SendKeys sends keystrokes to a DOM element.

func (*Browser) SetTimeout

func (b *Browser) SetTimeout(d time.Duration)

SetTimeout accepts a time.Duration. This duration will be used as the maximum timeout when waiting for a node to exist.

Jump to

Keyboard shortcuts

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