update rm popup window (#202)
This commit is contained in:
@@ -38,7 +38,10 @@ func NewPublishImageAction(page *rod.Page) (*PublishAction, error) {
|
|||||||
pp.MustNavigate(urlOfPublic).MustWaitIdle().MustWaitDOMStable()
|
pp.MustNavigate(urlOfPublic).MustWaitIdle().MustWaitDOMStable()
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
mustClickPublishTab(page, "上传图文")
|
if err := mustClickPublishTab(page, "上传图文"); err != nil {
|
||||||
|
logrus.Errorf("点击上传图文 TAB 失败: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
@@ -77,62 +80,99 @@ func removePopCover(page *rod.Page) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 兜底:点击一下空位置吧
|
// 兜底:点击一下空位置吧
|
||||||
clickEmptyPosition(page) // 点击空位置
|
clickEmptyPosition(page)
|
||||||
}
|
}
|
||||||
|
|
||||||
func clickEmptyPosition(page *rod.Page) {
|
func clickEmptyPosition(page *rod.Page) {
|
||||||
x := 380 + rand.Intn(100)
|
x := 380 + rand.Intn(100)
|
||||||
y := 20 + rand.Intn(60)
|
y := 20 + rand.Intn(60)
|
||||||
page.Mouse.MustMoveTo(float64(x), float64(y)).MustClick(proto.InputMouseButtonLeft)
|
page.Mouse.MustMoveTo(float64(x), float64(y)).MustClick(proto.InputMouseButtonLeft)
|
||||||
logrus.Infof("点击空位置: x=%d, y=%d", x, y)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustClickPublishTab(page *rod.Page, tabname string) error {
|
func mustClickPublishTab(page *rod.Page, tabname string) error {
|
||||||
|
|
||||||
removePopCover(page) // 移除弹窗封面
|
|
||||||
|
|
||||||
page.MustElement(`div.upload-content`).MustWaitVisible()
|
page.MustElement(`div.upload-content`).MustWaitVisible()
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
deadline := time.Now().Add(15 * time.Second)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
createElems := page.MustElements("div.creator-tab")
|
tab, blocked, err := getTabElement(page, tabname)
|
||||||
|
|
||||||
// 过滤掉隐藏的元素
|
|
||||||
var visibleElems []*rod.Element
|
|
||||||
for _, elem := range createElems {
|
|
||||||
if isElementVisible(elem) {
|
|
||||||
visibleElems = append(visibleElems, elem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(visibleElems) == 0 {
|
|
||||||
return errors.Errorf("没有找到发布 TAB - %s", tabname)
|
|
||||||
}
|
|
||||||
|
|
||||||
var clicked bool
|
|
||||||
|
|
||||||
for _, elem := range visibleElems {
|
|
||||||
text, err := elem.Text()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("获取元素文本失败: %v", err)
|
logrus.Warnf("获取发布 TAB 元素失败: %v", err)
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if text == tabname {
|
|
||||||
if err := elem.Click(proto.InputMouseButtonLeft, 1); err != nil {
|
|
||||||
logrus.Errorf("点击元素失败: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
clicked = true
|
if tab == nil {
|
||||||
break
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if blocked {
|
||||||
|
logrus.Info("发布 TAB 被遮挡,尝试移除遮挡")
|
||||||
|
removePopCover(page)
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tab.Click(proto.InputMouseButtonLeft, 1); err != nil {
|
||||||
|
logrus.Warnf("点击发布 TAB 失败: %v", err)
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !clicked {
|
return errors.Errorf("没有找到发布 TAB - %s", tabname)
|
||||||
return errors.Errorf("没有找到发布 TAB - %s", tabname)
|
}
|
||||||
|
|
||||||
|
func getTabElement(page *rod.Page, tabname string) (*rod.Element, bool, error) {
|
||||||
|
elems, err := page.Elements("div.creator-tab")
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
for _, elem := range elems {
|
||||||
|
if !isElementVisible(elem) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
text, err := elem.Text()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Debugf("获取发布 TAB 文本失败: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.TrimSpace(text) != tabname {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
blocked, err := isElementBlocked(elem)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return elem, blocked, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isElementBlocked(elem *rod.Element) (bool, error) {
|
||||||
|
result, err := elem.Eval(`() => {
|
||||||
|
const rect = this.getBoundingClientRect();
|
||||||
|
if (rect.width === 0 || rect.height === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const x = rect.left + rect.width / 2;
|
||||||
|
const y = rect.top + rect.height / 2;
|
||||||
|
const target = document.elementFromPoint(x, y);
|
||||||
|
return !(target === this || this.contains(target));
|
||||||
|
}`)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.Value.Bool(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func uploadImages(page *rod.Page, imagesPaths []string) error {
|
func uploadImages(page *rod.Page, imagesPaths []string) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user