Skip to content

Add an example for how to use chromedp.NewRemoteAllocator. #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The following examples are currently available:
| [headers](/headers) | set a HTTP header on requests |
| [keys](/keys) | send key events to an element |
| [logic](/logic) | more complex logic beyond simple actions |
| [remote](/remote) | connect to an existing chromium instance using chromedp.NewRemoteAllocator |
| [screenshot](/screenshot) | take a screenshot of a specific element and of the entire browser viewport |
| [submit](/submit) | fill out and submit a form |
| [text](/text) | extract text from a specific element |
Expand Down
33 changes: 33 additions & 0 deletions remote/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Command remote demonstrating how to connect to an existing chromium instance using chromedp.NewRemoteAllocator
package main

import (
"context"
"flag"
"github.com/chromedp/chromedp"
"log"
)

func main() {
var devToolWsUrl string
flag.StringVar(&devToolWsUrl, "devtools-ws-url", "", "DevTools Websocket URL")
flag.Parse()

actxt, cancelActxt := chromedp.NewRemoteAllocator(context.Background(), devToolWsUrl)
defer cancelActxt()

ctxt, cancelCtxt := chromedp.NewContext(actxt) // create new tab
defer cancelCtxt() // close tab afterwards

var body string
if err := chromedp.Run(ctxt,
chromedp.Navigate("https://duckduckgo.com"),
chromedp.WaitVisible("#logo_homepage_link"),
chromedp.OuterHTML("html", &body),
); err != nil {
log.Fatalf("Failed getting body of duckduckgo.com: %v", err)
}

log.Println("Body of duckduckgo.com starts with:")
log.Println(body[0:100])
}