Chromedp element apparently not clicked

I am using chromedp to open a new tab (context) for a given link and most of the time, it works fine.

But, what I am trying to understand is why it sometimes does not. By does not, I mean that I have log messages before and after clicking the link and as far as I can tell, chromedp executes the click without error or delay:

2025/05/02 07:30:00 -0400 INF clicking: //*[@id=“home-screen”]/div[2]/section[5]/div[5]/div/ul/li[2]/a[1]
2025/05/02 07:30:00 -0400 INF clicked
2025/05/02 07:38:25 -0400 INF new instance


targetElementXpath := fmt.Sprintf("//*[@id=\"home-screen\"]/div[2]/section[5]/div[5]/div/ul/li[%d]/a[1]", i.Index)
	targetIDChannel := chromedp.WaitNewTarget(i.session.ctx, matchTabWithNonEmptyURL)

	timeLimitedCtx, timeLimitedCancel := context.WithTimeout(i.session.ctx, 5*time.Second)
	defer timeLimitedCancel()

	log.Info().Msgf("clicking: %s", targetElementXpath)
	err := chromedp.Run(timeLimitedCtx, chromedp.Click(targetElementXpath))
	if err != nil {
		return err
	}

	log.Info().Msg("clicked")
	newInstance, newCancelFunc := chromedp.NewContext(i.session.ctx, chromedp.WithTargetID(<-targetIDChannel))
	i.ctx = newInstance
	i.cancel = newCancelFunc

	log.Info().Msg("new instance")
	return nil

This is a snippet of the code that is producing the message above. From the logs, we can discern that it took 8 minutes and 25 seconds to return. That time is when I manually clicked the link thereby launching the new tab.

  1. how would chromedp click an element immediately, yet no tab was actually launched? As I am using the remote developer option, I can see the page and even though chrome is much faster than my eyes, the elements were there for me to see. I have had errors if the element didn’t exist.
  2. separately, I need to refactor the blocking code for how long I want to wait to receive the new session. Perhaps that will alleviate problem #1 because if for some reason chromedp did not actually trigger the click, then trying again might get it working.

The majority of the time, this works fine. It is rare that it does NOT work. I suppose that when it does not work, that there is something going on on the server-side of things.

While I’d like to build robust software that works to handle those scenarios, it is difficult to reproduce.