主代码文件go_chrome.go
package main
import (
"context"
"fmt"
"github.com/chromedp/chromedp"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"time"
)
/**
*chrome开启调试模式
*sudo /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
*谷歌相关文档
*https://developers.google.cn/web/tools/chrome-devtools/
*http://localhost:9222/json
*/
type config struct {
Ws_url string
}
//法定节假日
//https://github.com/kaich/holiday-cn
func getWsurl() string {
yml := "/Users/yan/git/gowork/verify/tool/chromedp_config.yml"
//yml := "./chromedp_config.yml"
if fileObj, err := os.Open(yml); err == nil {
defer fileObj.Close()
ymlBy, err := ioutil.ReadAll(fileObj)
if err != nil {
fmt.Println(err)
}
t := config{}
//把yaml形式的字符串解析成struct类型
err = yaml.Unmarshal(ymlBy, &t)
//fmt.Println(t)
if err != nil {
fmt.Println(err)
}
//fmt.Println(t.Ws_url)
return t.Ws_url
} else {
fmt.Println(err)
}
return ""
}
func main() {
//flagDevToolWsUrl := "ws://localhost:9222/devtools/page/3148DC17BB55B4DBA9D15CC318410321"
flagDevToolWsUrl := getWsurl()
// create allocator context for use with creating a browser context later
allocatorContext, cancel := chromedp.NewRemoteAllocator(context.Background(), flagDevToolWsUrl)
defer cancel()
// create context
ctxt, cancel := chromedp.NewContext(allocatorContext)
//defer cancel()
// list awesome go projects for the "Selenium and browser control tools."
err := publish(ctxt, "Selenium and browser control tools.")
if err != nil {
log.Fatalf("could not list awesome go projects: %v", err)
}
}
func publish(ctx context.Context, sect string) error {
// force max timeout of 15 seconds for retrieving and processing the data
var cancel func()
ctx, cancel = context.WithTimeout(ctx, 60*time.Second)
defer cancel()
goto_url:=""//改成即将访问的链接
if err := chromedp.Run(ctx, chromedp.Navigate(goto_url)); err != nil {
return fmt.Errorf("could not navigate to : %v", err)
}
// wait visible
if err := chromedp.Run(ctx, chromedp.WaitVisible("/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[1]/div[1]/label[1]/input")); err != nil {
return fmt.Errorf("wait visible err: %v", err)
}
var front_checked bool
var cron_checked bool
if err := chromedp.Run(ctx, chromedp.Evaluate(`
var element = document.evaluate("/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[1]/div[1]/label[1]/input",document).iterateNext();
element.checked`, &front_checked)); err != nil {
return fmt.Errorf("front_checked err: %v", err)
}
if err := chromedp.Run(ctx, chromedp.Evaluate(`
var element = document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div:nth-child(4) > div:nth-child(1) > label:nth-child(3) > input")
element.checked`, &cron_checked)); err != nil {
return fmt.Errorf("cron_checked err: %v", err)
}
var private_data_checked bool
chromedp.Sleep(5 * time.Second)
if err := chromedp.Run(ctx, chromedp.Evaluate(`
var element = document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div:nth-child(4) > div:nth-child(1) > label.ng-scope > input")
element.checked`, &private_data_checked)); err != nil {
return fmt.Errorf("private_data_checked err: %v", err)
}
if !front_checked {
if err := chromedp.Run(ctx, chromedp.Click(`document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div:nth-child(4) > div:nth-child(1) > label:nth-child(1) > input")`, chromedp.ByJSPath)); err != nil {
return fmt.Errorf("front click err: %v", err)
}
}
if !cron_checked {
if err := chromedp.Run(ctx, chromedp.Click(`document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div:nth-child(4) > div:nth-child(1) > label:nth-child(3) > input")`, chromedp.ByJSPath)); err != nil {
return fmt.Errorf("cron click err: %v", err)
}
}
var increment_publish bool
if err := chromedp.Run(ctx, chromedp.Evaluate(`
var element = document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div:nth-child(4) > div:nth-child(2) > label > input:nth-child(4)")
element.checked`, &increment_publish)); err != nil {
return fmt.Errorf("increment publish checked err : %v", err)
}
if !increment_publish {
if err := chromedp.Run(ctx, chromedp.Click(`document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div:nth-child(4) > div:nth-child(2) > label > input:nth-child(4)")`, chromedp.ByJSPath)); err != nil {
return fmt.Errorf("increment publish click: %v", err)
}
}
chromedp.Sleep(3 * time.Second)
if err := chromedp.Run(ctx, chromedp.Click(`document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div:nth-child(9) > div.col-md-1.fl > div:nth-child(2) > a > img")`, chromedp.ByJSPath)); err != nil {
return fmt.Errorf("start click: %v", err)
}
// wait visible
if err := chromedp.Run(ctx, chromedp.WaitVisible(`document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div.row.col-md-12.ng-scope > div > div > div > div.panel-body > div.col-md-4 > button")`, chromedp.ByJSPath)); err != nil {
return fmt.Errorf("Wait sure button Visible err: %v", err)
}
fmt.Println("close mask")
//关闭弹层
if err := chromedp.Run(ctx, chromedp.Click(`document.querySelector("body")`, chromedp.ByJSPath)); err != nil {
return fmt.Errorf("click close mask err: %v", err)
}
//chromedp.Sleep(5 * time.Second)
//停止
// if err := chromedp.Run(ctx, chromedp.Click(`document.querySelector("body > div.container.body > div > div.right_col > div > div > div.panel.panel-default > div > div > div > div > div:nth-child(6) > div.col-md-4.ng-scope > button")`, chromedp.ByJSPath));err !=nil {
// return nil, fmt.Errorf("could not get section: %v", err)
// }
//通过
if err := chromedp.Run(ctx, chromedp.Click(`/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[6]/div/div/div/div[2]/div[1]/button`)); err != nil {
//if err := chromedp.Run(ctx, chromedp.Click(`/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[6]/div/div/div/div[2]/div[1]`)); err != nil {
return fmt.Errorf("pass click err: %v", err)
}
chromedp.Sleep(2 * time.Second)
//通过
//if err := chromedp.Run(ctx, chromedp.Click(`/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[6]/div/div/div/div[2]/div[1]/button`)); err != nil {
if err := chromedp.Run(ctx, chromedp.Click(`/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[6]/div/div/div/div[2]/div[1]`)); err != nil {
return fmt.Errorf("pass click err: %v", err)
}
chromedp.Sleep(2 * time.Second)
//通过
//if err := chromedp.Run(ctx, chromedp.Click(`/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[6]/div/div/div/div[2]/div[1]/button`)); err != nil {
if err := chromedp.Run(ctx, chromedp.Click(`/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[6]/div/div/div/div[2]/div[1]`)); err != nil {
return fmt.Errorf("pass click err: %v", err)
}
//通过
if err := chromedp.Run(ctx, chromedp.DoubleClick(`/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[6]/div/div/div/div[2]/div[1]`)); err != nil {
return fmt.Errorf("pass click err: %v", err)
}
chromedp.Sleep(2 * time.Second)
fmt.Println("finish")
//不通过
//if err := chromedp.Run(ctx, chromedp.Click(`/html/body/div[1]/div/div[3]/div/div/div[2]/div/div/div/div/div[6]/div/div/div/div[2]/div[2]`)); err != nil {
// return nil, fmt.Errorf("could not get section: %v", err)
//}
chromedp.Sleep(5 * time.Second)
return nil
}
配置文件
chromedp_config.yml
ws_url: ws://localhost:9222/devtools/page/F208607C441C3BD0C7EE76FC72C852F0
执行go run go_chrome.go