diff --git a/nav_test.go b/nav_test.go index e0973d92..b13755aa 100644 --- a/nav_test.go +++ b/nav_test.go @@ -276,16 +276,23 @@ func TestQueryIframe(t *testing.T) { ctx, cancel := testAllocate(t, "iframe.html") defer cancel() - var iframes []*cdp.Node + var iframes, forms []*cdp.Node if err := Run(ctx, Nodes(`iframe`, &iframes, ByQuery)); err != nil { t.Fatal(err) } + iframe := iframes[0] + if err := Run(ctx, Nodes(`#form`, &forms, ByQuery, FromNode(iframe))); err != nil { + t.Fatal(err) + } + form := forms[0] + var gotFoo string if err := Run(ctx, - WaitVisible(`#form`, ByQuery, FromNode(iframes[0])), - Text("#foo", &gotFoo, ByQuery, FromNode(iframes[0])), + WaitVisible(`#form`, ByQuery, FromNode(iframe)), + Text("#foo", &gotFoo, ByQuery, FromNode(form)), - Click("#btn2", ByQuery, FromNode(iframes[0])), + Click("#btn1", ByQuery, FromNode(iframe)), + Click("#btn2", ByQuery, FromNode(form)), ); err != nil { t.Fatal(err) } diff --git a/query.go b/query.go index 48e370fa..86968925 100644 --- a/query.go +++ b/query.go @@ -188,7 +188,8 @@ func (s *Selector) Do(ctx context.Context) error { continue } } else { - execCtx = t.execContexts[fromNode.FrameID] + frameID := t.enclosingFrame(fromNode) + execCtx = t.execContexts[frameID] t.frameMu.RUnlock() // TODO: we probably want to use the nested frame @@ -288,6 +289,9 @@ type QueryOption = func(*Selector) // FromNode is an element query action option where a query will be run. That // is, the query will only look at the node's element sub-tree. By default, or // when passed nil, the document's root element will be used. +// +// Note that, at present, BySearch and ByJSPath do not support FromNode; this +// option is mainly useful for ByQuery selectors. func FromNode(node *cdp.Node) QueryOption { return func(s *Selector) { s.fromNode = node } }