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)
? ? }
}
- 1 回答
- 0 關注
- 115 瀏覽
添加回答
舉報