白衣非少年
使用伪类:first-child或: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, `<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) }}