-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
73 lines (62 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"log"
"os"
"time"
cdp "github.com/knq/chromedp"
"github.com/knq/chromedp/kb"
)
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
var val1, val2, val3, val4 string
err = c.Run(ctxt, sendkeys(&val1, &val2, &val3, &val4))
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)
}
log.Printf("#input1 value: %s", val1)
log.Printf("#textarea1 value: %s", val2)
log.Printf("#input2 value: %s", val3)
log.Printf("#select1 value: %s", val4)
}
func sendkeys(val1, val2, val3, val4 *string) cdp.Tasks {
//因为使用了vendor,所以这里要计算一下vendor的路径
path, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
finalPath := "file:" + path + "/../vendor/github.com/knq/chromedp/testdata/visible.html"
return cdp.Tasks{
//cdp.Navigate("file:" + os.Getenv("GOPATH") + "/src/github.com/knq/chromedp/testdata/visible.html"),
cdp.Navigate(finalPath),
cdp.WaitVisible(`#input1`, cdp.ByID),
cdp.WaitVisible(`#textarea1`, cdp.ByID),
cdp.SendKeys(`#textarea1`, kb.End+"\b\b\n\naoeu\n\ntest1\n\nblah2\n\n\t\t\t\b\bother box!\t\ntest4", cdp.ByID),
cdp.Value(`#input1`, val1, cdp.ByID),
cdp.Value(`#textarea1`, val2, cdp.ByID),
cdp.SetValue(`#input2`, "test3", cdp.ByID),
cdp.Value(`#input2`, val3, cdp.ByID),
cdp.SendKeys(`#select1`, kb.ArrowDown+kb.ArrowDown, cdp.ByID),
cdp.Value(`#select1`, val4, cdp.ByID),
cdp.Sleep(30 * time.Second),
}
}