From 17072f0d4eab4adfc188477fc5ef1e6845051476 Mon Sep 17 00:00:00 2001 From: storyicon <475783185@qq.com> Date: Sun, 29 Jul 2018 17:23:23 +0800 Subject: [PATCH 1/2] add page.Wait() method --- page.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/page.go b/page.go index 76b8120..c8346f2 100644 --- a/page.go +++ b/page.go @@ -249,6 +249,26 @@ func (p *Page) HTML() (string, error) { return html, nil } +//Wait method will cause the browser to spend up to timeout seconds +//waiting for the element described by the selector to appear. +//Method for testing or crawling Javascript-modified web pages (eg Ajax) +// Simple example: +// page.Wait(`//*[@id="ajax-loaded"]`) +// fmt.Println(page.HTML()) +func (p *Page) Wait(selector string, timeout int64) *Selection { + start := time.Now().Unix() + for { + if time.Now().Unix()-start > timeout { + return nil + } + node := p.FindByXPath(selector) + if num, err := node.Count(); err == nil && num > 0 { + return node + } + time.Sleep(100 * time.Millisecond) + } +} + // RunScript runs the JavaScript provided in the body. Any keys present in // the arguments map will be available as variables in the body. // Values provided in arguments are converted into javascript objects. From 33033d299029b6ea53cea66d83ad5c14d5945f3e Mon Sep 17 00:00:00 2001 From: storyicon <475783185@qq.com> Date: Sun, 29 Jul 2018 17:42:34 +0800 Subject: [PATCH 2/2] fix example --- page.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/page.go b/page.go index c8346f2..e03175f 100644 --- a/page.go +++ b/page.go @@ -253,7 +253,7 @@ func (p *Page) HTML() (string, error) { //waiting for the element described by the selector to appear. //Method for testing or crawling Javascript-modified web pages (eg Ajax) // Simple example: -// page.Wait(`//*[@id="ajax-loaded"]`) +// page.Wait(`//*[@id="ajax-loaded"]`, 30) // fmt.Println(page.HTML()) func (p *Page) Wait(selector string, timeout int64) *Selection { start := time.Now().Unix()