Documentation
¶
Index ¶
- func CheckElement(site web.Site, elementXPath string, done chan error) error
- func GetPageSource(site web.Site) (string, error)
- func Init(headless bool, ignoreCertErrors bool) (web.Browser, error)
- func Navigate(site web.Site, actions []InputAction, waitTime time.Duration) error
- func ScreenShot(site web.Site, imgPath string) error
- type Driver
- type InputAction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckElement ¶
CheckElement checks if an element identified by a given XPath exists on the web page within a specified timeout. The function uses chromedp package to find the element on the page. If the element is found, it sends an error to a provided channel indicating that the account is locked out. The function waits up to 10 seconds for the element to appear on the page before timing out. It also introduces a random wait time between 2 to 6 seconds before navigating to the site.
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, failed to create random wait time, 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)
}
}()
Note: Make sure to handle the error sent to the "done" channel in a separate goroutine or after calling this function to avoid deadlock.
func GetPageSource ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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"),
}