go-extractor/node.go

82 lines
1.5 KiB
Go

package extractor
import (
"github.com/playwright-community/playwright-go"
)
type Node interface {
Content() (string, error)
Text() (string, error)
Attr(name string) (string, error)
Screenshot() ([]byte, error)
Type(input string) error
Click() error
Select(selector string) Nodes
SelectFirst(selector string) Node
ForEach(selector string, fn func(Node) error) error
}
type node struct {
locator playwright.Locator
}
func (n node) Type(input string) error {
return n.locator.Type(input)
}
func (n node) Click() error {
return n.locator.Click()
}
func (n node) Content() (string, error) {
return n.locator.TextContent()
}
func (n node) Text() (string, error) {
return n.locator.InnerText()
}
func (n node) Attr(name string) (string, error) {
return n.locator.GetAttribute(name)
}
func (n node) Screenshot() ([]byte, error) {
return n.locator.Screenshot()
}
func (n node) Select(selector string) Nodes {
elements, err := n.locator.Locator(selector).All()
if err != nil {
return nil
}
var nodes Nodes
for _, element := range elements {
nodes = append(nodes, node{locator: element})
}
return nodes
}
func (n node) SelectFirst(selector string) Node {
return n.Select(selector).First()
}
func (n node) ForEach(selector string, fn func(Node) error) error {
elements, err := n.locator.Locator(selector).All()
if err != nil {
return err
}
for _, element := range elements {
if err := fn(node{locator: element}); err != nil {
return err
}
}
return nil
}