chromedp: "context canceled" error when running within an IDE

What versions are you running?

Golang version : 1.11.5

What did you do? Include clear steps.

i just wanted to use chromedp in back-end application. i run this code thousand of times and in line err := chromedp.Run(ctx,tasks(usrcrd)) error will be set to context canceled.

my source code is :

package main

import (
	"context"
	"fmt"
	"github.com/chromedp/chromedp"
	"log"
	"net/http"
	"time"
)

type userCred struct {
	Username string
	Password string
}
func tasks(cred userCred) chromedp.Tasks {
	username := `'` + cred.Username + `'`
	password := `'` + cred.Password + `'`
	var inter interface{}
	return chromedp.Tasks{
		chromedp.Navigate(`https://news.ycombinator.com/submit`),
		chromedp.Sleep(5 * time.Second),
		chromedp.EvaluateAsDevTools(`document.getElementsByName('acct')[1].value =`+username, &inter, chromedp.EvalAsValue),
		chromedp.Sleep(1 * time.Second),
		chromedp.EvaluateAsDevTools(`document.getElementsByName('pw')[1].value =`+password, &inter, chromedp.EvalAsValue),
		chromedp.Sleep(1 * time.Second),
		chromedp.EvaluateAsDevTools(`document.querySelector("input[value='create account']").click()`, &inter, chromedp.EvalAsValue),
	}
}
func hndlr (w http.ResponseWriter, r *http.Request) {
	var usrcrd userCred
	usrcrd.Username = "test";
	usrcrd.Password = "test";
	ctx , _ := chromedp.NewContext(context.Background())
	err := chromedp.Run(ctx,tasks(usrcrd))
	if err != nil {
		log.Fatal(err) // Context canceled here
	}
	_,_ = fmt.Fprintf(w, "Fine")

}
func main() {
	http.HandleFunc("/",hndlr)
	_ = http.ListenAndServe("0.0.0.0:8080",nil)
}

just send a GET request to localhost:8080 and hndlr handler will handle your request.

What did you expect to see?

i expect to see fine message instead of this. why context canceled? is there any race condition ?

What did you see instead?

program terminated with error message : context canceled

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

use any browser instead using curl ! you can try open this URL in any browser => http://localhost:8080 i test exactly this code thousand times just for god damn you say this code happens error !