MCPcopy Create free account
hub / github.com/chromedp/chromedp / Example_retrieveHTML

Function Example_retrieveHTML

example_test.go:378–418  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

376}
377
378func Example_retrieveHTML() {
379 ctx, cancel := chromedp.NewContext(context.Background())
380 defer cancel()
381
382 ts := httptest.NewServer(writeHTML(`
383<body>
384<p id="content" onclick="changeText()">Original content.</p>
385<script>
386function changeText() {
387 document.getElementById("content").textContent = "New content!"
388}
389</script>
390</body>
391 `))
392 defer ts.Close()
393
394 if err := chromedp.Do(ctx, chromedp.Navigate(ts.URL)); err != nil {
395 log.Fatal(err)
396 }
397 outerBefore, err := chromedp.Run(ctx, chromedp.OuterHTML(chromedp.CSS("#content")))
398 if err != nil {
399 log.Fatal(err)
400 }
401 if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#content"))); err != nil {
402 log.Fatal(err)
403 }
404 outerAfter, err := chromedp.Run(ctx, chromedp.OuterHTML(chromedp.CSS("#content")))
405 if err != nil {
406 log.Fatal(err)
407 }
408 fmt.Println("OuterHTML before clicking:")
409 fmt.Println(outerBefore)
410 fmt.Println("OuterHTML after clicking:")
411 fmt.Println(outerAfter)
412
413 // Output:
414 // OuterHTML before clicking:
415 // <p id="content" onclick="changeText()">Original content.</p>
416 // OuterHTML after clicking:
417 // <p id="content" onclick="changeText()">New content!</p>
418}
419
420func ExampleEmulate() {
421 ctx, cancel := chromedp.NewContext(context.Background())

Callers

nothing calls this directly

Calls 9

NewContextFunction · 0.92
DoFunction · 0.92
NavigateFunction · 0.92
RunFunction · 0.92
OuterHTMLFunction · 0.92
CSSTypeAlias · 0.92
ClickFunction · 0.92
writeHTMLFunction · 0.70
CloseMethod · 0.45

Tested by

no test coverage detected