I’m utilizing chromedp to launch a brand new browser context when opening a hyperlink in a brand new tab. Whereas debugging this, I’m operating remotely so I can see visually see what’s current on the display screen.
This can be a separate challenge, however though the hyperlink is current and on the identical xpath as I’m utilizing, my app hangs indefinitely. So, I attempted to merely wrap the present context with a context.WithTimeout(…), however am having points with that.
I’m presently wrapping the WaitVisible operate with an ActionFunc and processing the timeout there. Lastly, I exploit a choose block to both get the goal id of the newly opened tab or a timeout, whichever happens first.
That is presently my code:
func (i Occasion) tryLaunch() error {
log.Information().Msgf(“launching occasion: %d → %d @ [%s]”, i, i.Index, motion.Location(i.session.ctx))
targetElementXpath := fmt.Sprintf(“//[@id=”home-screen”]/div[2]/part[5]/div[5]/div/ul/li[%d]/a[1]”, i.Index)
targetIDChannel := chromedp.WaitNewTarget(i.session.ctx, matchTabWithNonEmptyURL)waitTime := 5*time.Second
err := chromedp.Run(i.session.ctx, chromedp.ActionFunc(func(ctx context.Context) error {
timeLimitedCtx, timeLimitedCancel := context.WithTimeout(i.session.ctx, waitTime)
defer timeLimitedCancel()log.Information().Msgf("ready for goal component to be seen: %s", targetElementXpath) return chromedp.WaitVisible(targetElementXpath).Do(timeLimitedCtx)
}),
chromedp.Click on(targetElementXpath),
)if err != nil {
return err
}log.Information().Msg(“initializing new context from new tab”)
choose {
case targetId := <-targetIDChannel:
newInstance, newCancelFunc := chromedp.NewContext(i.session.ctx, chromedp.WithTargetID(targetId))
i.ctx = newInstance
i.cancel = newCancelFunc
return nil
case <-time.After(waitTime):
log.Warn().Msg(“timed out ready for targetID”)
return errors.New(“operation timed out”)
}
}
Previous to this block of code, I attempted to merely move within the timeLimitedContext with out wrapping WaitVisible in an ActionFunc. When carried out that approach, it appeared to haven’t any impact by any means. Within the present state it’s in, I find yourself with this error:
panic: interface conversion: interface is nil, not cdp.Executor
That error happens on this line:
err := chromedp.Run(i.session.ctx, chromedp.ActionFunc(func(ctx context.Context) error {
Thanks,