chromedp / chromedp

A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.
MIT License
10.95k stars 787 forks source link

context deadline exceeded #919

Closed jw-star closed 3 years ago

jw-star commented 3 years ago

How can I avoid this mistake

baerwang commented 3 years ago
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
jw-star commented 3 years ago
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
    c, cancel := chromedp.NewExecAllocator(context.Background(), options...)

    defer cancel()
    c, cancel = context.WithTimeout(c, 30*time.Second)
    // create context
    c, cancel = chromedp.NewContext(c, chromedp.WithLogf(log.Printf))
    defer cancel()
    // 执行一个空task, 用提前创建Chrome实例
    var res string
    err = chromedp.Run(c, setheaders(
        "91porn.com",
        map[string]interface{}{
            "Accept-Language": "zh-cn,zh;q=0.5",
            "X-Forwarded-For":  genIpaddr(),
        },
        &res,
    ))

    //创建一个上下文,超时时间为40s
    c, cancel = context.WithTimeout(c,100*time.Second)
    defer cancel()
    var videoInfo entity.VideoInfo
    err = chromedp.Run(c,
        chromedp.Navigate(url),
        //chromedp.WaitVisible(`body > table > tbody > tr > td > a`),
        chromedp.WaitEnabled(`body > table > tbody > tr > td > a`),
        chromedp.Sleep(2*time.Second),
        chromedp.Click(`body > table > tbody > tr > td > a`,chromedp.BySearch),
        chromedp.Sleep(2*time.Second),
        chromedp.WaitVisible(selector, chromedp.ByQuery),
        //标题
        chromedp.TextContent("#videodetails > h4", &videoInfo.Title, chromedp.ByQuery),
        //收藏数
        chromedp.TextContent("#useraction > div:nth-child(1) > span:nth-child(4) > span",
            &videoInfo.ScCount, chromedp.ByQuery),
        //作者
        chromedp.TextContent("#videodetails-content > div:nth-child(2) > span.title-yakov > a:nth-child(1) > span",
            &videoInfo.Author, chromedp.ByQuery),
        chromedp.OuterHTML(sel, &videoInfo.HtmlContent,chromedp.ByQuery),

    )

I have configured these, but sometimes there will still be errors

baerwang commented 3 years ago

Do not create a new ctx repeatedly in the context

jw-star commented 3 years ago

Do not create a new ctx repeatedly in the context

Excuse me, where is the duplicate creation