2017-01-24 15:09:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
cdp "github.com/knq/chromedp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// create context
|
|
|
|
ctxt, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// create chrome instance
|
|
|
|
c, err := cdp.New(ctxt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run task list
|
2017-02-08 16:01:35 +00:00
|
|
|
var res string
|
|
|
|
err = c.Run(ctxt, submit(`https://github.com/search`, `//input[@name="q"]`, `chromedp`, &res))
|
2017-01-24 15:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// shutdown chrome
|
|
|
|
err = c.Shutdown(ctxt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for chrome to finish
|
|
|
|
err = c.Wait()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-02-08 16:01:35 +00:00
|
|
|
|
|
|
|
log.Printf("got: `%s`", res)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:01:35 +00:00
|
|
|
func submit(urlstr, sel, q string, res *string) cdp.Tasks {
|
2017-01-24 15:09:23 +00:00
|
|
|
return cdp.Tasks{
|
|
|
|
cdp.Navigate(urlstr),
|
2017-02-08 16:01:35 +00:00
|
|
|
cdp.WaitVisible(sel),
|
|
|
|
cdp.SendKeys(sel, q),
|
|
|
|
cdp.Submit(sel),
|
2017-02-08 16:16:28 +00:00
|
|
|
cdp.WaitNotPresent(`//*[@id="code_search"]/h2/svg`),
|
2017-02-08 16:01:35 +00:00
|
|
|
cdp.Text(`//*[@id="js-pjax-container"]/div[2]/div/div[2]/ul/li/p`, res),
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
}
|