12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package main
- import (
- "fmt"
- "time"
- "github.com/sclevine/agouti"
- )
- func main() {
- // cd := agouti.ChromeDriver()
- // cd := agouti.PhantomJS()
- // cd := agouti.Selenium()
- cd := agouti.Selendroid("/Users/marion/go/src/github.com/tebeka/selenium/vendor/selenium-server-standalone-3.14.0.jar")
- err := cd.Start()
- if err != nil {
- fmt.Println("Start webDriver:", err)
- return
- }
- defer cd.Stop()
- pg, err := cd.NewPage(agouti.Browser("chrome"))
- if err != nil {
- fmt.Println("NewPage:", err)
- return
- }
- err = pg.SetPageLoad(10000)
- if err != nil {
- fmt.Println("SetPageLoad:", err)
- return
- }
- err = pg.SetScriptTimeout(1000)
- if err != nil {
- fmt.Println("SetScriptTimeout:", err)
- return
- }
- err = pg.Navigate("https://www.baidu.com/")
- if err != nil {
- fmt.Println("Navigate:", err)
- return
- }
- for {
- title, err := pg.Title()
- if err != nil {
- fmt.Println("Title:", err)
- return
- }
- fmt.Println(title)
- sc := pg.FindByLink("新闻")
- // str, err := sc.Text()
- // if err != nil {
- // fmt.Println("str:", err)
- // return
- // }
- // fmt.Println(str)
- err = sc.Click()
- if err != nil {
- fmt.Println("click:", err)
- return
- }
- title, err = pg.Title()
- if err != nil {
- fmt.Println("Title2:", err)
- return
- }
- fmt.Println(title)
- //sleep
- time.Sleep(2 * time.Second)
- err = pg.Back()
- if err != nil {
- fmt.Println("Back:", err)
- return
- }
- //sleep
- time.Sleep(2 * time.Second)
- }
- }
|