Updating edge-simple example (WIP)
This commit is contained in:
parent
f557f48716
commit
705f3be8e5
|
@ -4,11 +4,14 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/knq/chromedp"
|
cdp "github.com/knq/chromedp"
|
||||||
|
cdptypes "github.com/knq/chromedp/cdp"
|
||||||
"github.com/knq/chromedp/client"
|
"github.com/knq/chromedp/client"
|
||||||
//"github.com/knq/chromedp/runner"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -25,40 +28,51 @@ func main() {
|
||||||
|
|
||||||
// create edge instance
|
// create edge instance
|
||||||
watch := client.New().WatchPageTargets(ctxt)
|
watch := client.New().WatchPageTargets(ctxt)
|
||||||
if err != nil {
|
c, err := chromedp.New(ctxt, chromedp.WithTargets(watch))
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
cdp, err := chromedp.New(ctxt, chromedp.WithTargets(watch))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// run task list
|
// run task list
|
||||||
var res string
|
var site, res string
|
||||||
err = googleSearch(cdp, "site:brank.as", &res).Do(ctxt)
|
err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("got attributes: `%s`", res)
|
|
||||||
|
|
||||||
// shutdown chrome
|
// shutdown chrome
|
||||||
return
|
err = c.Shutdown(ctxt)
|
||||||
err = cdp.Shutdown(ctxt)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cdp.Wait()
|
// wait for chrome to finish
|
||||||
|
err = c.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("saved screenshot of #testimonials from search result listing `%s` (%s)", res, site)
|
||||||
}
|
}
|
||||||
|
|
||||||
func googleSearch(c *chromedp.CDP, q string, res *string) chromedp.Tasks {
|
func googleSearch(q, text string, site, res *string) cdp.Tasks {
|
||||||
return chromedp.Tasks{
|
var buf []byte
|
||||||
c.Navigate(`https://www.google.com`),
|
sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
|
||||||
c.WaitVisible(`#hplogo`),
|
return cdp.Tasks{
|
||||||
c.AttributeValue(`#hplogo`, "title", res, nil),
|
cdp.Navigate(`https://www.google.com`),
|
||||||
|
cdp.Sleep(2 * time.Second),
|
||||||
|
cdp.WaitVisible(`#hplogo`, cdp.ByID),
|
||||||
|
cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
|
||||||
|
cdp.WaitVisible(`#res`, cdp.ByID),
|
||||||
|
cdp.Text(sel, res),
|
||||||
|
cdp.Click(sel),
|
||||||
|
cdp.Sleep(2 * time.Second),
|
||||||
|
cdp.WaitVisible(`#footer`, cdp.ByQuery),
|
||||||
|
cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
|
||||||
|
cdp.Location(site),
|
||||||
|
cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
|
||||||
|
cdp.ActionFunc(func(context.Context, cdptypes.FrameHandler) error {
|
||||||
|
return ioutil.WriteFile("testimonials.png", buf, 0644)
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user