I have a page with multiple text areas consisting of dynamic names and the same class. This means I can't select them by id, name, class or type.
All I know is that out of the 5 textareas, I need the first one, and I want to change the value of that textarea.
Can anyone tell me how to do this using chromedp? Been trying for two days with no progress.
Find the answer:
const n = document.querySelector('.elementor-repeater-fields:nth-child(2) textarea'); console.log(n);
Use pseudo-class :first-child a> or :nth-child to select the target element. For example:
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) } }
The above is the detailed content of chromedp How to select specific textarea from multiple textareas with dynamic names. For more information, please follow other related articles on the PHP Chinese website!