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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-rod/rod"
|
"github.com/go-rod/rod"
|
||||||
@@ -102,8 +103,11 @@ func submitPublish(page *rod.Page, title, content string) error {
|
|||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
contentElem := page.MustElement("div.ql-editor")
|
if contentElem, ok := getContentElement(page); ok {
|
||||||
contentElem.MustInput(content)
|
contentElem.MustInput(content)
|
||||||
|
} else {
|
||||||
|
return errors.New("没有找到内容输入框")
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
@@ -114,3 +118,87 @@ func submitPublish(page *rod.Page, title, content string) error {
|
|||||||
|
|
||||||
return nil
|
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)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = action.Publish(context.Background(), PublishImageContent{
|
err = action.Publish(context.Background(), PublishImageContent{
|
||||||
Title: "Claude移动端重大更新!随时随地高效办公",
|
Title: "Hello World",
|
||||||
Content: "Claude移动端重大更新!随时随地高效办公",
|
Content: "Hello World",
|
||||||
ImagePaths: []string{"1948784311265894447"},
|
ImagePaths: []string{"/tmp/1.jpg"},
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user