cdpu

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 12 Imported by: 0

README

goutils/v2/web/cdpu

The goutils/v2/web/cdpu package is a part of goutils library.

It provides utility functions to driver headless browsers using chromeDP.


Functions

CheckElement
func CheckElement(site cdpchrome.Site, elementXPath string, done chan error) error

CheckElement checks if an element identified by a given XPath exists on the cdpchrome page.

Init
func Init(headless bool, ignoreCertErrors bool) (cdpchrome.Browser, error)

This function initializes a Google Chrome browser instance with the specified headless mode and SSL certificate error ignoring options. It creates contexts and associated cancel functions for browser operation.

GetPageSource
func GetPageSource(site cdpchrome.Site) (string, error)

Retrieves the HTML source code of the currently loaded page in a site session.

Navigate
func Navigate(site cdpchrome.Site, actions []InputAction, waitTime time.Duration) error

Navigates a site using provided actions. It enables network events and sets up request logging.

ScreenShot
func ScreenShot(site cdpchrome.Site, imgPath string) error

ScreenShot takes a screenshot of the input targetURL and saves it to imgPath.


Installation

To use the goutils/v2/cdpchrome package, you need to install it via go get:

go get github.com/l50/goutils/v2/cdpchrome

Usage

After installation, you can import it in your project:

import "github.com/l50/goutils/v2/cdpchrome"

Tests

To run the tests for the goutils/v2/cdpchrome package, navigate to your $GOPATH/src/github.com/l50/goutils/v2/cdpchrome directory and run go test:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckElement

func CheckElement(site web.Site, elementXPath string, done chan error) error

CheckElement checks if an element identified by a given XPath exists on the web page. If the element is found, it assumes the account is locked out and returns an error. The function waits up to 10 seconds for the element to appear on the page before timing out.

Parameters:

site: A web.Site struct representing the site to be checked. elementXPath: A string representing the XPath of the element to be checked. done: A channel to which the function sends an error if the element is found or another error occurs.

Returns:

error: An error if the element is found, the web driver is not of type *Driver, or another error occurs.

Example:

site := web.NewSite("http://example.com") done := make(chan error)

go func() {
	if err := web.CheckElement(site, "//input[@id='username']", done); err != nil {
		log.Fatalf("CheckElement failed: %v", err)
	}
}()

func GetPageSource

func GetPageSource(site web.Site) (string, error)

GetPageSource retrieves the source code of the web page currently loaded in the site session.

This function will return the HTML source code of the currently loaded page in the provided Site's session.

Parameters:

site (web.Site): The site whose source code is to be retrieved.

Returns:

string: The source code of the currently loaded page.

error: An error if any occurred during source code retrieval.

Example:

site := web.Site{
  // initialize site
}

source, err := cdpchrome.GetPageSource(site)

if err != nil {
  log.Fatalf("failed to get page source: %v", err)
}

func Init

func Init(headless bool, ignoreCertErrors bool) (web.Browser, error)

Init returns a chrome browser instance.

This function initializes a chrome browser instance with the specified headless mode and SSL certificate error ignoring options. The browser instance is then returned for further operations.

Parameters:

headless (bool): Whether or not the browser should be in headless mode.

ignoreCertErrors (bool): Whether or not SSL certificate errors should be ignored.

Returns:

web.Browser: A Browser instance which has been initialized.

error: Any encountered error during initialization.

Example:

browser, err := cdpchrome.Init(true, true)

if err != nil {
  log.Fatalf("failed to initialize a chrome browser: %v", err)
}
func Navigate(site web.Site, actions []InputAction, waitTime time.Duration) error

Navigate navigates an input site using the provided InputActions.

This function will perform the provided actions sequentially on the provided Site's session. It enables network events and sets up request logging.

Parameters:

site (web.Site): The site on which the actions should be performed.

actions ([]InputAction): A slice of InputAction objects which define the actions to be performed.

waitTime (time.Duration): The time to wait between actions.

Returns:

error: An error if any occurred during navigation.

Example:

actions := []cdpchrome.InputAction{
  // initialize actions
}

err := cdpchrome.Navigate(site, actions, 1000)

if err != nil {
  log.Fatalf("failed to navigate site: %v", err)
}

func ScreenShot

func ScreenShot(site web.Site, imgPath string) error

ScreenShot takes a screenshot of the input targetURL and saves it to imgPath.

This function captures a screenshot of the currently loaded page in the provided Site's session and writes the image data to the provided file path.

Parameters:

site (web.Site): The site whose page a screenshot should be taken of.

imgPath (string): The path to which the screenshot should be saved.

Returns:

error: An error if any occurred during screenshot capturing or saving.

Example:

err := cdpchrome.ScreenShot(site, "/path/to/save/image.png")

if err != nil {
  log.Fatalf("failed to capture screenshot: %v", err)
}

Types

type Driver

type Driver struct {
	Context context.Context
	Options *[]chromedp.ExecAllocatorOption
}

Driver represents an interface to Google Chrome using go.

It contains a context.Context associated with this Driver and Options for the execution of Google Chrome.

Example:

browser, err := cdpchrome.Init(true, true)

if err != nil {
  log.Fatalf("failed to initialize a chrome browser: %v", err)
}

driver := browser.Driver

func (*Driver) GetContext

func (d *Driver) GetContext() context.Context

GetContext returns the context associated with the Driver instance.

This function retrieves the context that's linked with the current Driver.

Returns:

context.Context: The context that's associated with this Driver.

Example:

ctx := driver.GetContext()

if ctx == nil {
  log.Fatalf("Context is nil")
}

func (*Driver) SetContext

func (d *Driver) SetContext(ctx context.Context)

SetContext sets a new context for the Driver instance.

This function sets a new context to be associated with the current Driver.

Parameters:

ctx (context.Context): The new context to be associated with this Driver.

Example:

newCtx := context.Background() driver.SetContext(newCtx)

if driver.GetContext() != newCtx {
  log.Fatalf("Failed to set new context")
}

type InputAction

type InputAction struct {
	Description string
	Selector    string
	Action      chromedp.Action
	Context     context.Context
}

InputAction represents selectors and actions to run with chrome.

It contains a description, a selector to find an element on the page, and an chromedp.Action which defines the action to perform on the selected element.

Example:

action := cdpchrome.InputAction{
  Description: "Type in search box",
  Selector:    "#searchbox",
  Action:      chromedp.SendKeys("#searchbox", "example search"),
}

Jump to

Keyboard shortcuts

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