From 6eec61e7df254037125c44cb8ada896496a5af51 Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Wed, 8 Feb 2017 23:16:28 +0700 Subject: [PATCH] Adding ElementNotPresent/WaitNotPresent query option/action --- examples/submit/main.go | 1 + sel.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/examples/submit/main.go b/examples/submit/main.go index 36f00af..3020e5e 100644 --- a/examples/submit/main.go +++ b/examples/submit/main.go @@ -48,6 +48,7 @@ func submit(urlstr, sel, q string, res *string) cdp.Tasks { cdp.WaitVisible(sel), cdp.SendKeys(sel, q), cdp.Submit(sel), + cdp.WaitNotPresent(`//*[@id="code_search"]/h2/svg`), cdp.Text(`//*[@id="js-pjax-container"]/div[2]/div/div[2]/ul/li/p`, res), } } diff --git a/sel.go b/sel.go index 967b37a..53b1755 100644 --- a/sel.go +++ b/sel.go @@ -27,6 +27,7 @@ tagname // Error types. var ( ErrNoResults = errors.New("no results") + ErrHasResults = errors.New("has results") ErrNotVisible = errors.New("not visible") ErrVisible = errors.New("visible") ErrDisabled = errors.New("disabled") @@ -483,6 +484,18 @@ func ElementSelected(s *Selector) { }))(s) } +// ElementNotPresent is a query option to wait until no elements match are +// present matching the selector. +func ElementNotPresent(s *Selector) { + s.exp = 0 + WaitFunc(func(ctxt context.Context, h cdp.FrameHandler, n *cdp.Node, ids ...cdp.NodeID) ([]*cdp.Node, error) { + if len(ids) != 0 { + return nil, ErrHasResults + } + return []*cdp.Node{}, nil + })(s) +} + // AtLeast is a query option to wait until at least n elements are returned // from the query selector. func AtLeast(n int) QueryOption { @@ -525,6 +538,11 @@ func WaitSelected(sel interface{}, opts ...QueryOption) Action { return Query(sel, append(opts, ElementSelected)...) } +// WaitNotPresent waits until no elements match the specified selector. +func WaitNotPresent(sel interface{}, opts ...QueryOption) Action { + return Query(sel, append(opts, ElementNotPresent)...) +} + const ( // visibleJS is a javascript snippet that returns true or false depending // on if the specified node's offsetParent is not null.