Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix(click): treat a destroyed page context after a link click as the navigation landing, not a retryable failure

Luigi Agostia107006d1ddd
diff --git a/internal/bridge/cdpops/pointer.go b/internal/bridge/cdpops/pointer.go
index 2995cd1a..29ad2fe7 100644
--- a/internal/bridge/cdpops/pointer.go
+++ b/internal/bridge/cdpops/pointer.go
@@ -9,6 +9,7 @@ import (
"strings"
"time"
+ "github.com/chromedp/cdproto"
"github.com/chromedp/cdproto/input"
"github.com/chromedp/chromedp"
)
@@ -383,11 +384,24 @@ func jsClickIfLink(ctx context.Context, nodeID int64) error {
el = el.parentElement;
}
}`
- return chromedp.FromContext(ctx).Target.Execute(ctx,
+ err := chromedp.FromContext(ctx).Target.Execute(ctx,
"Runtime.callFunctionOn", map[string]any{
"functionDeclaration": js,
"objectId": obj.Object.ObjectID,
}, nil)
+ if linkContextGone(err) {
+ // The trusted press already followed the link and the old document's
+ // context is gone: the click landed, so there is nothing left to do.
+ return nil
+ }
+ return err
+}
+
+func linkContextGone(err error) bool {
+ var protocolErr *cdproto.Error
+ return errors.As(err, &protocolErr) && protocolErr.Code == -32000 &&
+ (protocolErr.Message == "Cannot find context with specified id" ||
+ protocolErr.Message == "Execution context was destroyed.")
}
func DoubleClickByCoordinate(ctx context.Context, x, y float64) error {
diff --git a/internal/bridge/cdpops/pointer_test.go b/internal/bridge/cdpops/pointer_test.go
index 6f18a2ed..c363a4a6 100644
--- a/internal/bridge/cdpops/pointer_test.go
+++ b/internal/bridge/cdpops/pointer_test.go
@@ -3,6 +3,7 @@ package cdpops
import (
"context"
"errors"
+ "fmt"
"go/ast"
"go/parser"
"go/token"
@@ -10,6 +11,7 @@ import (
"strings"
"testing"
+ "github.com/chromedp/cdproto"
"github.com/chromedp/cdproto/input"
)
@@ -421,3 +423,25 @@ func TestTheButtonTableIsTheOnlyPlaceAButtonFactIsWritten(t *testing.T) {
t.Errorf("mouseButtonCode(input.None) = %d, want %d", got, noButtonJSCode)
}
}
+
+// A link click whose trusted press already navigated leaves the old document's
+// context gone before jsClickIfLink's .click() runs. That is the click landing,
+// not failing: reported as an error it became a retryable 500, and a retry would
+// click on the page the link led to.
+func TestLinkContextGoneOnlyMatchesADestroyedContext(t *testing.T) {
+ for _, tc := range []struct {
+ err error
+ want bool
+ }{
+ {&cdproto.Error{Code: -32000, Message: "Cannot find context with specified id"}, true},
+ {fmt.Errorf("click: %w", &cdproto.Error{Code: -32000, Message: "Execution context was destroyed."}), true},
+ {&cdproto.Error{Code: -32000, Message: "Node is detached from document"}, false},
+ {&cdproto.Error{Code: -32602, Message: "Cannot find context with specified id"}, false},
+ {errors.New("Cannot find context with specified id"), false},
+ {nil, false},
+ } {
+ if got := linkContextGone(tc.err); got != tc.want {
+ t.Errorf("linkContextGone(%v) = %v, want %v", tc.err, got, tc.want)
+ }
+ }
+}