亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號