forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsugar.go
749 lines (642 loc) · 19.7 KB
/
sugar.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// This file contains the methods that panics when error return value is not nil.
package rod
import (
"time"
"github.com/go-rod/rod/lib/devices"
"github.com/go-rod/rod/lib/proto"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/ysmood/kit"
)
// Connect to the browser and start to control it.
// If fails to connect, try to run a local browser, if local browser not found try to download one.
func (b *Browser) Connect() *Browser {
kit.E(b.ConnectE())
return b
}
// Close the browser and release related resources
func (b *Browser) Close() {
_ = b.CloseE()
}
// Incognito creates a new incognito browser
func (b *Browser) Incognito() *Browser {
b, err := b.IncognitoE()
kit.E(err)
return b
}
// Page creates a new tab
// If url is empty, the default target will be "about:blank".
func (b *Browser) Page(url string) *Page {
p, err := b.PageE(url)
kit.E(err)
return p
}
// Pages returns all visible pages
func (b *Browser) Pages() Pages {
list, err := b.PagesE()
kit.E(err)
return list
}
// PageFromTargetID creates a Page instance from a targetID
func (b *Browser) PageFromTargetID(targetID proto.TargetTargetID) *Page {
p, err := b.PageFromTargetIDE(targetID)
kit.E(err)
return p
}
// HandleAuth for the next basic HTTP authentication.
// It will prevent the popup that requires user to input user name and password.
// Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
func (b *Browser) HandleAuth(username, password string) {
wait := b.HandleAuthE(username, password)
go func() { kit.E(wait()) }()
}
// FindByURL returns the page that has the url that matches the regex
func (ps Pages) FindByURL(regex string) *Page {
p, err := ps.FindByURLE(regex)
kit.E(err)
return p
}
// Info of the page, such as the URL or title of the page
func (p *Page) Info() *proto.TargetTargetInfo {
info, err := p.InfoE()
kit.E(err)
return info
}
// Cookies returns the page cookies. By default it will return the cookies for current page.
// The urls is the list of URLs for which applicable cookies will be fetched.
func (p *Page) Cookies(urls ...string) []*proto.NetworkCookie {
cookies, err := p.CookiesE(urls)
kit.E(err)
return cookies
}
// SetCookies of the page.
// Cookie format: https://chromedevtools.github.io/devtools-protocol/tot/Network#method-setCookie
func (p *Page) SetCookies(cookies ...*proto.NetworkCookieParam) *Page {
kit.E(p.SetCookiesE(cookies))
return p
}
// SetExtraHeaders whether to always send extra HTTP headers with the requests from this page.
// The arguments are key-value pairs, you can set multiple key-value pairs at the same time.
func (p *Page) SetExtraHeaders(dict ...string) (cleanup func()) {
cleanup, err := p.SetExtraHeadersE(dict)
kit.E(err)
return cleanup
}
// SetUserAgent Allows overriding user agent with the given string.
// If req is nil, the default user agent will be the same as a mac chrome.
func (p *Page) SetUserAgent(req *proto.NetworkSetUserAgentOverride) *Page {
kit.E(p.SetUserAgentE(req))
return p
}
// Navigate to url
// If url is empty, it will navigate to "about:blank".
func (p *Page) Navigate(url string) *Page {
kit.E(p.NavigateE(url))
return p
}
// GetWindow get window bounds
func (p *Page) GetWindow() *proto.BrowserBounds {
bounds, err := p.GetWindowE()
kit.E(err)
return bounds
}
// Window set the window location and size
func (p *Page) Window(left, top, width, height int64) *Page {
kit.E(p.WindowE(&proto.BrowserBounds{
Left: left,
Top: top,
Width: width,
Height: height,
WindowState: proto.BrowserWindowStateNormal,
}))
return p
}
// WindowMinimize the window
func (p *Page) WindowMinimize() *Page {
kit.E(p.WindowE(&proto.BrowserBounds{
WindowState: proto.BrowserWindowStateMinimized,
}))
return p
}
// WindowMaximize the window
func (p *Page) WindowMaximize() *Page {
kit.E(p.WindowE(&proto.BrowserBounds{
WindowState: proto.BrowserWindowStateMaximized,
}))
return p
}
// WindowFullscreen the window
func (p *Page) WindowFullscreen() *Page {
kit.E(p.WindowE(&proto.BrowserBounds{
WindowState: proto.BrowserWindowStateFullscreen,
}))
return p
}
// WindowNormal the window size
func (p *Page) WindowNormal() *Page {
kit.E(p.WindowE(&proto.BrowserBounds{
WindowState: proto.BrowserWindowStateNormal,
}))
return p
}
// Viewport overrides the values of device screen dimensions.
func (p *Page) Viewport(width, height int64, deviceScaleFactor float64, mobile bool) *Page {
kit.E(p.ViewportE(&proto.EmulationSetDeviceMetricsOverride{
Width: width,
Height: height,
DeviceScaleFactor: deviceScaleFactor,
Mobile: mobile,
}))
return p
}
// Emulate the device, such as iPhone9. If device is empty, it will clear the override.
func (p *Page) Emulate(device devices.DeviceType) *Page {
kit.E(p.EmulateE(device, false))
return p
}
// StopLoading forces the page stop all navigations and pending resource fetches.
func (p *Page) StopLoading() *Page {
kit.E(p.StopLoadingE())
return p
}
// Close page
func (p *Page) Close() {
kit.E(p.CloseE())
}
// HandleDialog accepts or dismisses next JavaScript initiated dialog (alert, confirm, prompt, or onbeforeunload)
func (p *Page) HandleDialog(accept bool, promptText string) (wait func()) {
w := p.HandleDialogE(accept, promptText)
return func() {
kit.E(w())
}
}
// Screenshot the page and returns the binary of the image
// If the toFile is "", it will save output to "tmp/screenshots" folder, time as the file name.
func (p *Page) Screenshot(toFile ...string) []byte {
bin, err := p.ScreenshotE(false, &proto.PageCaptureScreenshot{})
kit.E(err)
kit.E(saveScreenshot(bin, toFile))
return bin
}
// ScreenshotFullPage including all scrollable content and returns the binary of the image.
func (p *Page) ScreenshotFullPage(toFile ...string) []byte {
bin, err := p.ScreenshotE(true, &proto.PageCaptureScreenshot{})
kit.E(err)
kit.E(saveScreenshot(bin, toFile))
return bin
}
// PDF prints page as PDF
func (p *Page) PDF() []byte {
pdf, err := p.PDFE(&proto.PagePrintToPDF{})
kit.E(err)
return pdf
}
// WaitOpen to be created from a new window
func (p *Page) WaitOpen() (wait func() *Page) {
w := p.WaitOpenE()
return func() *Page {
page, err := w()
kit.E(err)
return page
}
}
// Pause stops on the next JavaScript statement
func (p *Page) Pause() *Page {
kit.E(p.PauseE())
return p
}
// WaitRequestIdle returns a wait function that waits until the page doesn't send request for 300ms.
// You can pass regular expressions to exclude the requests by their url.
func (p *Page) WaitRequestIdle(excludes ...string) (wait func()) {
return p.WaitRequestIdleE(300*time.Millisecond, []string{""}, excludes)
}
// WaitIdle wait until the next window.requestIdleCallback is called.
func (p *Page) WaitIdle() *Page {
kit.E(p.WaitIdleE(time.Minute))
return p
}
// WaitLoad wait until the `window.onload` is complete, resolve immediately if already fired.
func (p *Page) WaitLoad() *Page {
kit.E(p.WaitLoadE())
return p
}
// AddScriptTag to page. If url is empty, content will be used.
func (p *Page) AddScriptTag(url string) *Page {
kit.E(p.AddScriptTagE(url, ""))
return p
}
// AddStyleTag to page. If url is empty, content will be used.
func (p *Page) AddStyleTag(url string) *Page {
kit.E(p.AddStyleTagE(url, ""))
return p
}
// EvalOnNewDocument Evaluates given script in every frame upon creation (before loading frame's scripts).
func (p *Page) EvalOnNewDocument(js string) {
_, err := p.EvalOnNewDocumentE(js)
kit.E(err)
}
// Eval js on the page. The first param must be a js function definition.
// For example page.Eval(`n => n + 1`, 1) will return 2
func (p *Page) Eval(js string, params ...interface{}) proto.JSON {
res, err := p.EvalE(true, "", js, params)
kit.E(err)
return res.Value
}
// Wait js function until it returns true
func (p *Page) Wait(js string, params ...interface{}) {
kit.E(p.WaitE(Sleeper(), "", js, params))
}
// ObjectToJSON by remote object
func (p *Page) ObjectToJSON(obj *proto.RuntimeRemoteObject) proto.JSON {
j, err := p.ObjectToJSONE(obj)
kit.E(err)
return j
}
// ObjectsToJSON by remote objects
func (p *Page) ObjectsToJSON(list []*proto.RuntimeRemoteObject) proto.JSON {
result := "[]"
for _, obj := range list {
j, err := p.ObjectToJSONE(obj)
kit.E(err)
result, err = sjson.SetRaw(result, "-1", j.Raw)
kit.E(err)
}
return proto.JSON{Result: gjson.Parse(result)}
}
// ElementFromNode creates an Element from the node id
func (p *Page) ElementFromNode(id proto.DOMNodeID) *Element {
el, err := p.ElementFromNodeE(id)
kit.E(err)
return el
}
// Release remote object
func (p *Page) Release(objectID proto.RuntimeRemoteObjectID) *Page {
kit.E(p.ReleaseE(objectID))
return p
}
// Has an element that matches the css selector
func (p *Page) Has(selector string) bool {
has, err := p.HasE(selector)
kit.E(err)
return has
}
// HasX an element that matches the XPath selector
func (p *Page) HasX(selector string) bool {
has, err := p.HasXE(selector)
kit.E(err)
return has
}
// HasMatches an element that matches the css selector and its text matches the regex.
func (p *Page) HasMatches(selector, regex string) bool {
has, err := p.HasMatchesE(selector, regex)
kit.E(err)
return has
}
// Search for a given query in the DOM tree until the result count is not zero.
// The query can be plain text or css selector or xpath.
// It will search nested iframes and shadow doms too.
func (p *Page) Search(query string) *Element {
list, err := p.SearchE(Sleeper(), query, 0, 1)
kit.E(err)
return list.First()
}
// Element retries until an element in the page that matches one of the CSS selectors
func (p *Page) Element(selectors ...string) *Element {
el, err := p.ElementE(Sleeper(), "", selectors)
kit.E(err)
return el
}
// ElementMatches retries until an element in the page that matches one of the pairs.
// Each pairs is a css selector and a regex. A sample call will look like page.ElementMatches("div", "click me").
// The regex is the js regex, not golang's.
func (p *Page) ElementMatches(pairs ...string) *Element {
el, err := p.ElementMatchesE(Sleeper(), "", pairs)
kit.E(err)
return el
}
// ElementByJS retries until returns the element from the return value of the js function
func (p *Page) ElementByJS(js string, params ...interface{}) *Element {
el, err := p.ElementByJSE(Sleeper(), "", js, params)
kit.E(err)
return el
}
// Elements returns all elements that match the css selector
func (p *Page) Elements(selector string) Elements {
list, err := p.ElementsE("", selector)
kit.E(err)
return list
}
// ElementsX returns all elements that match the XPath selector
func (p *Page) ElementsX(xpath string) Elements {
list, err := p.ElementsXE("", xpath)
kit.E(err)
return list
}
// ElementX retries until an element in the page that matches one of the XPath selectors
func (p *Page) ElementX(xPaths ...string) *Element {
el, err := p.ElementXE(Sleeper(), "", xPaths)
kit.E(err)
return el
}
// ElementsByJS returns the elements from the return value of the js
func (p *Page) ElementsByJS(js string, params ...interface{}) Elements {
list, err := p.ElementsByJSE("", js, params)
kit.E(err)
return list
}
// Move to the absolute position
func (m *Mouse) Move(x, y float64) *Mouse {
kit.E(m.MoveE(x, y, 0))
return m
}
// Scroll with the relative offset
func (m *Mouse) Scroll(x, y float64) *Mouse {
kit.E(m.ScrollE(x, y, 0))
return m
}
// Down holds the button down
func (m *Mouse) Down(button proto.InputMouseButton) *Mouse {
kit.E(m.DownE(button, 1))
return m
}
// Up release the button
func (m *Mouse) Up(button proto.InputMouseButton) *Mouse {
kit.E(m.UpE(button, 1))
return m
}
// Click will press then release the button
func (m *Mouse) Click(button proto.InputMouseButton) *Mouse {
kit.E(m.ClickE(button))
return m
}
// Down holds key down
func (k *Keyboard) Down(key rune) *Keyboard {
kit.E(k.DownE(key))
return k
}
// Up releases the key
func (k *Keyboard) Up(key rune) *Keyboard {
kit.E(k.UpE(key))
return k
}
// Press a key
func (k *Keyboard) Press(key rune) *Keyboard {
kit.E(k.PressE(key))
return k
}
// InsertText like paste text into the page
func (k *Keyboard) InsertText(text string) *Keyboard {
kit.E(k.InsertTextE(text))
return k
}
// Describe returns the element info
// Returned json: https://chromedevtools.github.io/devtools-protocol/tot/DOM#type-Node
func (el *Element) Describe() *proto.DOMNode {
node, err := el.DescribeE(1, false)
kit.E(err)
return node
}
// ShadowRoot returns the shadow root of this element
func (el *Element) ShadowRoot() *Element {
node, err := el.ShadowRootE()
kit.E(err)
return node
}
// Focus sets focus on the specified element
func (el *Element) Focus() *Element {
kit.E(el.FocusE())
return el
}
// ScrollIntoView scrolls the current element into the visible area of the browser
// window if it's not already within the visible area.
func (el *Element) ScrollIntoView() *Element {
kit.E(el.ScrollIntoViewE())
return el
}
// Click the element
func (el *Element) Click() *Element {
kit.E(el.ClickE(proto.InputMouseButtonLeft))
return el
}
// Press a key
func (el *Element) Press(key rune) *Element {
kit.E(el.PressE(key))
return el
}
// SelectText selects the text that matches the regular expression
func (el *Element) SelectText(regex string) *Element {
kit.E(el.SelectTextE(regex))
return el
}
// SelectAllText selects all text
func (el *Element) SelectAllText() *Element {
kit.E(el.SelectAllTextE())
return el
}
// Input wll click the element and input the text.
// To empty the input you can use something like el.SelectAllText().Input("")
func (el *Element) Input(text string) *Element {
kit.E(el.InputE(text))
return el
}
// Blur will call the blur function on the element.
// On inputs, this will deselect the element.
func (el *Element) Blur() *Element {
kit.E(el.BlurE())
return el
}
// Select the option elements that match the selectors, the selector can be text content or css selector
func (el *Element) Select(selectors ...string) *Element {
kit.E(el.SelectE(selectors))
return el
}
// Matches checks if the element can be selected by the css selector
func (el *Element) Matches(selector string) bool {
res, err := el.MatchesE(selector)
kit.E(err)
return res
}
// Attribute returns the requested attribute's value of the element
// if the attribute is not found, it will return an empty string.
func (el *Element) Attribute(name string) *string {
attr, err := el.AttributeE(name)
kit.E(err)
return attr
}
// Property returns the value of the element's property as a json format.
// If the property is not found, it will return a null JSON object.
func (el *Element) Property(name string) proto.JSON {
prop, err := el.PropertyE(name)
kit.E(err)
return prop
}
// SetFiles sets files for the given file input element
func (el *Element) SetFiles(paths ...string) *Element {
kit.E(el.SetFilesE(paths))
return el
}
// Text gets the innerText of the element
func (el *Element) Text() string {
s, err := el.TextE()
kit.E(err)
return s
}
// HTML gets the outerHTML of the element
func (el *Element) HTML() string {
s, err := el.HTMLE()
kit.E(err)
return s
}
// Visible returns true if the element is visible on the page
func (el *Element) Visible() bool {
v, err := el.VisibleE()
kit.E(err)
return v
}
// WaitStable waits until the size and position are stable. Useful when waiting for the animation of modal
// or button to complete so that we can simulate the mouse to move to it and click on it.
func (el *Element) WaitStable() *Element {
kit.E(el.WaitStableE(100 * time.Millisecond))
return el
}
// Wait until the js returns true
func (el *Element) Wait(js string, params ...interface{}) *Element {
kit.E(el.WaitE(js, params))
return el
}
// WaitVisible until the element is visible
func (el *Element) WaitVisible() *Element {
kit.E(el.WaitVisibleE())
return el
}
// WaitInvisible until the element is not visible or removed
func (el *Element) WaitInvisible() *Element {
kit.E(el.WaitInvisibleE())
return el
}
// Box returns the size of an element and its position relative to the main frame.
// It will recursively calculate the box with all ancestors. The spec is here:
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
func (el *Element) Box() *Box {
box, err := el.BoxE()
kit.E(err)
return box
}
// CanvasToImage get image data of a canvas.
// The default format is image/png.
// The default quality is 0.92.
// doc: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
func (el *Element) CanvasToImage(format string, quality float64) []byte {
bin, err := el.CanvasToImageE(format, quality)
kit.E(err)
return bin
}
// Resource returns the binary of the "src" properly, such as the image or audio file.
func (el *Element) Resource() []byte {
bin, err := el.ResourceE()
kit.E(err)
return bin
}
// Screenshot of the area of the element
func (el *Element) Screenshot(toFile ...string) []byte {
bin, err := el.ScreenshotE(proto.PageCaptureScreenshotFormatPng, -1)
kit.E(err)
kit.E(saveScreenshot(bin, toFile))
return bin
}
// Release remote object on browser
func (el *Element) Release() {
kit.E(el.ReleaseE())
}
// Eval evaluates js function on the element, the first param must be a js function definition
// For example: el.Eval(`name => this.getAttribute(name)`, "value")
func (el *Element) Eval(js string, params ...interface{}) proto.JSON {
res, err := el.EvalE(true, js, params)
kit.E(err)
return res.Value
}
// Has an element that matches the css selector
func (el *Element) Has(selector string) bool {
has, err := el.HasE(selector)
kit.E(err)
return has
}
// HasX an element that matches the XPath selector
func (el *Element) HasX(selector string) bool {
has, err := el.HasXE(selector)
kit.E(err)
return has
}
// HasMatches an element that matches the css selector and its text matches the regex.
func (el *Element) HasMatches(selector, regex string) bool {
has, err := el.HasMatchesE(selector, regex)
kit.E(err)
return has
}
// Element returns the first child that matches the css selector
func (el *Element) Element(selector string) *Element {
el, err := el.ElementE(selector)
kit.E(err)
return el
}
// ElementX returns the first child that matches the XPath selector
func (el *Element) ElementX(xpath string) *Element {
el, err := el.ElementXE(xpath)
kit.E(err)
return el
}
// ElementByJS returns the element from the return value of the js
func (el *Element) ElementByJS(js string, params ...interface{}) *Element {
el, err := el.ElementByJSE(js, params)
kit.E(err)
return el
}
// Parent returns the parent element
func (el *Element) Parent() *Element {
parent, err := el.ParentE()
kit.E(err)
return parent
}
// Parents that match the selector
func (el *Element) Parents(selector string) Elements {
list, err := el.ParentsE(selector)
kit.E(err)
return list
}
// Next returns the next sibling element
func (el *Element) Next() *Element {
parent, err := el.NextE()
kit.E(err)
return parent
}
// Previous returns the previous sibling element
func (el *Element) Previous() *Element {
parent, err := el.PreviousE()
kit.E(err)
return parent
}
// ElementMatches returns the first element in the page that matches the CSS selector and its text matches the regex.
// The regex is the js regex, not golang's.
func (el *Element) ElementMatches(selector, regex string) *Element {
el, err := el.ElementMatchesE(selector, regex)
kit.E(err)
return el
}
// Elements returns all elements that match the css selector
func (el *Element) Elements(selector string) Elements {
list, err := el.ElementsE(selector)
kit.E(err)
return list
}
// ElementsX returns all elements that match the XPath selector
func (el *Element) ElementsX(xpath string) Elements {
list, err := el.ElementsXE(xpath)
kit.E(err)
return list
}
// ElementsByJS returns the elements from the return value of the js
func (el *Element) ElementsByJS(js string, params ...interface{}) Elements {
list, err := el.ElementsByJSE(js, params)
kit.E(err)
return list
}