为了账号安全,请及时绑定邮箱和手机立即绑定

chromedp 如何从多个具有动态名称的文本区域中选择特定的文本区域

chromedp 如何从多个具有动态名称的文本区域中选择特定的文本区域

Go
哆啦的时光机 2023-03-15 15:44:21
我有一个包含多个文本区域的页面,这些文本区域由动态名称和相同的类组成。这意味着我无法通过 ID、名称、类或类型来选择它们。我所知道的是,在 5 个文本区域中,我需要第一个并且我想更改它的值。谁能告诉我如何使用 chromedp 做到这一点?试了两天,没有任何进展。找到答案:const n = document.querySelector('.elementor-repeater-fields:nth-child(2) textarea'); 控制台日志(n);
查看完整描述

1 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

使用伪类:first-child或:nth-child来选择目标元素。例如:

package main


import (

    "context"

    "fmt"

    "net/http"

    "net/http/httptest"

    "time"


    "github.com/chromedp/chromedp"

)


func main() {

    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

        fmt.Fprint(w, `

<html>

  <body>

    <textarea></textarea>

    <textarea></textarea>

    <textarea></textarea>

  </body>

</html>

`)

    }))

    defer ts.Close()


    opts := append(chromedp.DefaultExecAllocatorOptions[:],

        chromedp.Flag("headless", false),

    )

    ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)

    defer cancel()

    ctx, cancel = chromedp.NewContext(ctx)

    defer cancel()


    err := chromedp.Run(ctx,

        chromedp.Navigate(ts.URL),

        chromedp.Sleep(time.Second),

        chromedp.SetValue(`body>textarea:first-child`, "hello world!", chromedp.ByQuery),

        chromedp.Sleep(time.Second),

        chromedp.SetValue(`body>textarea:nth-child(2)`, "hello chromedp!", chromedp.ByQuery),

        chromedp.Sleep(3*time.Second),

    )

    if err != nil {

        panic(err)

    }

}


查看完整回答
反对 回复 2023-03-15
  • 1 回答
  • 0 关注
  • 184 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号