ego008 avatar

go selenium较好的库

🕞 by ego008

在go+selenium 上踩了不少坑,推荐下面这个比较完善的库 https://github.com/tebeka/selenium 支持chrome、safari、firefox、IE

另外还有: https://github.com/sclevine/agouti https://github.com/sourcegraph/go-selenium

放在一起的例子:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main

import (
	agwd "github.com/sclevine/agouti"
	tbwd "github.com/tebeka/selenium"
	"log"
	sgwd "sourcegraph.com/sourcegraph/go-selenium"
)

// Helper for sourcegraph/go-selenium
type fatalfer struct{}

func (fatalfer) Fatalf(fmt string, v ...interface{}) {
	panic([]interface{}{fmt, v})
}

var errorhandler fatalfer

// Helper for tebeka/selenium and agouti
func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	defer func() {
		r := recover()

		// Catch the errors from the tebeka/selenium and agouti helpers
		err, is_error := r.(error)
		if is_error {
			log.Fatal(err)
		}

		// Catch the errors from the sourcegraph/go-selenium helper
		arr, is_array := r.([]interface{})
		if is_array {
			fmt := arr[0].(string)
			v := arr[1].([]interface{})
			log.Fatalf(fmt, v...)
		}
	}()

	//sourcegraphExample()
	//tebekaExample()
	//agoutiExample()
}

func sourcegraphExample() {

	// Pro: .T
	// Con: Doesn't report the full error

	var err error
	wdRaw, err := sgwd.NewRemote(sgwd.Capabilities(map[string]interface{}{"browserName": "chrome"}), "http://localhost:4444/wd/hub")
	checkError(err)
	driver := wdRaw.T(errorhandler)
	defer driver.Quit()

	// Make sure the debug panel doesn't overlay the login
	driver.ResizeWindow(driver.CurrentWindowHandle(), sgwd.Size{1200, 500})

	driver.Get("http://localhost/shop")

	driver.FindElement(sgwd.ByCSSSelector, "header.header li.login a").Click()
	mailInput := driver.FindElement(sgwd.ByCSSSelector, "section.login-area input[name=email_address]")
	mailInput.Click()
	mailInput.SendKeys("info@example.com")
	passwordInput := driver.FindElement(sgwd.ByCSSSelector, "section.login-area input[name=password]")
	passwordInput.Click()
	passwordInput.SendKeys("xxx" + sgwd.EnterKey)

	// go-selenium doesn't seem to have explicit wait
	driver.SetImplicitWaitTimeout(30000)

	// Wait for login to complete (either the "My Account" button or the confirm address popup appears)
	driver.FindElement(sgwd.ByCSSSelector, "header.header li.account a, a[data-widget=confirm_address_link]")

	// Go to "My Account" page
	driver.FindElement(sgwd.ByCSSSelector, "header.header li.account a").Click()
}

func tebekaExample() {

	// Pro: Easier capabilities, provides full error info
	// Con: No .T

	var err error

	driver, err := tbwd.NewRemote(tbwd.Capabilities{"browserName": "chrome"}, "http://localhost:4444/wd/hub")
	checkError(err)
	defer driver.Quit()

	// Make sure the debug panel doesn't overlay the login
	cwh, err := driver.CurrentWindowHandle()
	checkError(err)
	err = driver.ResizeWindow(cwh, 1200, 500)
	checkError(err)

	err = driver.Get("http://localhost/shop")
	checkError(err)

	loginButton, err := driver.FindElement(tbwd.ByCSSSelector, "header.header li.login a")
	checkError(err)
	err = loginButton.Click()
	mailInput, err := driver.FindElement(tbwd.ByCSSSelector, "section.login-area input[name=email_address]")
	checkError(err)
	err = mailInput.Click()
	checkError(err)
	err = mailInput.SendKeys("info@example.com")
	checkError(err)
	passwordInput, err := driver.FindElement(tbwd.ByCSSSelector, "section.login-area input[name=password]")
	checkError(err)
	err = passwordInput.SendKeys("xxx" + tbwd.EnterKey)
	checkError(err)

	// tebeka/selenium doesn't seem to have explicit wait
	err = driver.SetImplicitWaitTimeout(30000)
	checkError(err)

	// Wait for login to complete (either the "My Account" button or the confirm address popup appears)
	driver.FindElement(sgwd.ByCSSSelector, "header.header li.account a, a[data-widget=confirm_address_link]")

	// Go to "My Account" page
	accountButton, err := driver.FindElement(sgwd.ByCSSSelector, "header.header li.account a")
	checkError(err)
	err = accountButton.Click()
	checkError(err)
}

func agoutiExample() {
	var err error

	driver, err := agwd.NewPage("http://localhost:4444/wd/hub", agwd.Browser("chrome"))
	checkError(err)
	defer driver.Destroy()

	// Make sure the debug panel doesn't overlay the login
	err = driver.Size(1200, 500)
	checkError(err)

	err = driver.Navigate("http://localhost/shop")
	checkError(err)

	err = driver.Find("header.header li.login a").Click()
	checkError(err)
	mailInput := driver.Find("section.login-area input[name=email_address]")
	err = mailInput.Click()
	checkError(err)
	err = mailInput.SendKeys("info@example.com")
	checkError(err)
	passwordInput := driver.Find("section.login-area input[name=password]")
	err = passwordInput.SendKeys("xxx" + string('\ue007'))
	checkError(err)

	// agoudi doesn't seem to have explicit wait
	err = driver.SetImplicitWait(30000)
	checkError(err)

	// Wait for login to complete (either the "My Account" button or the confirm address popup appears)
	driver.Find("header.header li.account a, a[data-widget=confirm_address_link]")

	// Go to "My Account" page
	err = driver.Find("header.header li.account a").Click()
	checkError(err)
}

// forked from https://gist.github.com/AndreKR/213996c501ed9da927ede1ff7d23699c

写一条评论

Based on Golang + fastHTTP + sdb | go1.16.5 Processed in 1ms