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

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

问题内容

我有一个页面,其中有多个文本区域,这些文本区域由动态名称和相同的类组成。这意味着我无法通过 id、名称、类或类型来选择它们。

我所知道的是,在 5 个文本区域中,我需要第一个,并且我想更改该文本区域的值。

谁能告诉我如何使用 chromedp 做到这一点?尝试了两天了,没有任何进展。

找到答案:

const n = document.querySelector(‘.elementor-repeater-fields:nth-child(2) textarea’);控制台.log(n);

正确答案

使用伪类:first-child a> 或 :nth-child 进行选择目标元素。例如:

package mainimport (    "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, `      `)    }))    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)    }}

登录后复制

以上就是chromedp 如何从多个具有动态名称的文本区域中选择特定文本区域的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2485776.html

(0)
上一篇 2025年3月4日 21:51:14
下一篇 2025年3月4日 21:51:30

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论