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 SaveCookiesToDisk(site web.Site, filePath string) error
- func ScreenShot(site web.Site, imgPath string) error
- type Driver
- type InputAction
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckElement ¶
CheckElement checks if a web page element, identified by the provided XPath, exists within a specified timeout.
**Note:** Ensure to handle the error sent to the "done" channel in a separate goroutine or after calling this function to avoid deadlock.
**Parameters:**
site: A web.Site struct representing the target site. elementXPath: A string representing the XPath of the target element. done: A channel through 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 a random wait time, or another error occurs.
Example ¶
package main
import (
"log"
"time"
"github.com/l50/goutils/v2/web"
"github.com/l50/goutils/v2/web/cdpu"
)
func main() {
// Initialize the chrome browser
browser, err := cdpu.Init(true, true)
if err != nil {
log.Fatalf("failed to initialize a chrome browser: %v", err)
}
defer web.CancelAll(browser.Cancels...)
url := "https://somesite.com/login"
// Set up the site with the browser's driver
site := web.Site{
LoginURL: url,
Session: web.Session{
Driver: browser.Driver,
},
}
// Define the XPath for the element to check
elementXPath := "//button[@id='login']"
// Create a done channel
done := make(chan error)
// Call the function in a goroutine and wait for result
go func() {
err := cdpu.CheckElement(site, elementXPath, done)
if err != nil {
log.Printf("failed to execute CheckElement: %v", err)
}
}()
// Handle the result from the done channel
select {
case err := <-done:
if err != nil {
log.Printf("Element found or another error occurred: %v", err)
}
case <-time.After(10 * time.Second):
log.Println("Timeout exceeded while waiting for element check")
}
}
Output:
func GetPageSource ¶
GetPageSource retrieves 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 ¶
package main
import (
"log"
"github.com/l50/goutils/v2/web"
"github.com/l50/goutils/v2/web/cdpu"
)
func main() {
site := web.Site{
// initialize site
}
source, err := cdpu.GetPageSource(site)
if err != nil {
log.Fatalf("failed to get page source: %v", err)
}
_ = source
}
Output:
func Init ¶
Init initializes a chrome browser instance with the specified headless mode and SSL certificate error ignoring options, then returns the browser instance 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: An initialized Browser instance. error: Any error encountered during initialization.
Example ¶
package main
import (
"log"
"github.com/l50/goutils/v2/web/cdpu"
)
func main() {
browser, err := cdpu.Init(true, true)
if err != nil {
log.Fatalf("failed to initialize a chrome browser: %v", err)
}
_ = browser
}
Output:
func Navigate ¶
Navigate performs 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.
func SaveCookiesToDisk ¶ added in v2.1.7
SaveCookiesToDisk retrieves cookies from the current session and writes them to a file.
**Parameters:**
site (web.Site): The site from which to retrieve cookies. filePath (string): The file path where the cookies should be saved.
**Returns:**
error: An error if any occurred during cookie retrieval or file writing.
func ScreenShot ¶
ScreenShot 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 ¶
package main
import (
"log"
"github.com/l50/goutils/v2/web"
"github.com/l50/goutils/v2/web/cdpu"
)
func main() {
site := web.Site{
// initialize site
}
if err := cdpu.ScreenShot(site, "/path/to/save/image.png"); err != nil {
log.Fatalf("failed to capture screenshot: %v", err)
}
}
Output:
Types ¶
type Driver ¶
type Driver struct {
Context context.Context
Options *[]chromedp.ExecAllocatorOption
}
Driver is an interface to Google Chrome, containing a context.Context associated with this Driver and Options for the execution of Google Chrome.
**Attributes:**
Context: The context associated with this Driver. Options: The options for the execution of Google Chrome.
func (*Driver) GetContext ¶
GetContext retrieves the context associated with the Driver instance.
**Returns:**
context.Context: The context associated with this Driver.
Example ¶
package main
import (
"log"
"github.com/l50/goutils/v2/web/cdpu"
)
func main() {
d := &cdpu.Driver{}
ctx := d.GetContext()
if ctx == nil {
log.Fatalf("context is nil")
}
}
Output:
func (*Driver) SetContext ¶
SetContext associates a new context with the Driver instance.
**Parameters:**
ctx (context.Context): The new context to be associated with this Driver.
Example ¶
package main
import (
"context"
"log"
"github.com/l50/goutils/v2/web/cdpu"
)
func main() {
d := &cdpu.Driver{}
newCtx := context.Background()
d.SetContext(newCtx)
if d.GetContext() != newCtx {
log.Fatalf("failed to set new context")
}
}
Output:
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 a chromedp.Action which defines the action to perform on the selected element.
**Attributes:**
Description: A string that describes the action. Selector: The CSS selector of the element to perform the action on. Action: A chromedp.Action that defines what action to perform. Context: The context in which to execute the action.
Example ¶
package main
import (
"github.com/chromedp/chromedp"
"github.com/l50/goutils/v2/web/cdpu"
)
func main() {
action := cdpu.InputAction{
Description: "Type in search box",
Selector: "#searchbox",
Action: chromedp.SendKeys("#searchbox", "example search"),
}
_ = action
}
Output: