update find content element (#10)
- Updated the getContentElement function to utilize the Race method for improved efficiency in finding content elements. - Added error handling for cases where no elements are found, ensuring better logging and debugging capabilities.
This commit is contained in:
@@ -3,6 +3,7 @@ package xiaohongshu
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-rod/rod"
|
||||
@@ -102,8 +103,11 @@ func submitPublish(page *rod.Page, title, content string) error {
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
contentElem := page.MustElement("div.ql-editor")
|
||||
contentElem.MustInput(content)
|
||||
if contentElem, ok := getContentElement(page); ok {
|
||||
contentElem.MustInput(content)
|
||||
} else {
|
||||
return errors.New("没有找到内容输入框")
|
||||
}
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
@@ -114,3 +118,87 @@ func submitPublish(page *rod.Page, title, content string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 查找内容输入框 - 使用Race方法处理两种样式
|
||||
func getContentElement(page *rod.Page) (*rod.Element, bool) {
|
||||
var foundElement *rod.Element
|
||||
var found bool
|
||||
|
||||
page.Race().
|
||||
Element("div.ql-editor").MustHandle(func(e *rod.Element) {
|
||||
foundElement = e
|
||||
found = true
|
||||
}).
|
||||
ElementFunc(func(page *rod.Page) (*rod.Element, error) {
|
||||
return findTextboxByPlaceholder(page)
|
||||
}).MustHandle(func(e *rod.Element) {
|
||||
foundElement = e
|
||||
found = true
|
||||
}).
|
||||
MustDo()
|
||||
|
||||
if found {
|
||||
return foundElement, true
|
||||
}
|
||||
|
||||
slog.Warn("no content element found by any method")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func findTextboxByPlaceholder(page *rod.Page) (*rod.Element, error) {
|
||||
elements := page.MustElements("p")
|
||||
if elements == nil {
|
||||
return nil, errors.New("no p elements found")
|
||||
}
|
||||
|
||||
// 查找包含指定placeholder的元素
|
||||
placeholderElem := findPlaceholderElement(elements, "输入正文描述")
|
||||
if placeholderElem == nil {
|
||||
return nil, errors.New("no placeholder element found")
|
||||
}
|
||||
|
||||
// 向上查找textbox父元素
|
||||
textboxElem := findTextboxParent(placeholderElem)
|
||||
if textboxElem == nil {
|
||||
return nil, errors.New("no textbox parent found")
|
||||
}
|
||||
|
||||
return textboxElem, nil
|
||||
}
|
||||
|
||||
func findPlaceholderElement(elements []*rod.Element, searchText string) *rod.Element {
|
||||
for _, elem := range elements {
|
||||
placeholder, err := elem.Attribute("data-placeholder")
|
||||
if err != nil || placeholder == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(*placeholder, searchText) {
|
||||
return elem
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func findTextboxParent(elem *rod.Element) *rod.Element {
|
||||
currentElem := elem
|
||||
for i := 0; i < 5; i++ {
|
||||
parent, err := currentElem.Parent()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
role, err := parent.Attribute("role")
|
||||
if err != nil || role == nil {
|
||||
currentElem = parent
|
||||
continue
|
||||
}
|
||||
|
||||
if *role == "textbox" {
|
||||
return parent
|
||||
}
|
||||
|
||||
currentElem = parent
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -24,9 +24,9 @@ func TestPublish(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
err = action.Publish(context.Background(), PublishImageContent{
|
||||
Title: "Claude移动端重大更新!随时随地高效办公",
|
||||
Content: "Claude移动端重大更新!随时随地高效办公",
|
||||
ImagePaths: []string{"1948784311265894447"},
|
||||
Title: "Hello World",
|
||||
Content: "Hello World",
|
||||
ImagePaths: []string{"/tmp/1.jpg"},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user