Skip to content

Commit 16c253e

Browse files
committed
fix golang code
1 parent 77fdf89 commit 16c253e

10 files changed

+47
-31
lines changed

browsers-pages.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ To launch multiple browsers:
99
```go
1010
browser1 := rod.New().MustConnect()
1111
browser2 := rod.New().MustConnect()
12+
fmt.Println(browser1, browser2)
1213
```
1314

1415
All APIs are thread-safe, same works for multiple Go routines.
@@ -18,18 +19,20 @@ You can also use incognito mode to launch multiple browsers:
1819
```go
1920
browser1 := rod.New().MustConnect()
2021
browser2 := browser1.MustIncognito()
22+
fmt.Println(browser1, browser2)
2123
```
2224

2325
Launch browsers with different launch arguments:
2426

2527
```go
2628
browser1 := rod.New().ControlURL(
27-
launcher.New().Headless(false).MustLaunch()
29+
launcher.New().Headless(false).MustLaunch(),
2830
).MustConnect()
2931

30-
browser1 := rod.New().ControlURL(
31-
launcher.New().UserDataDir("path").MustLaunch()
32+
browser2 := rod.New().ControlURL(
33+
launcher.New().UserDataDir("path").MustLaunch(),
3234
).MustConnect()
35+
fmt.Println(browser1, browser2)
3336
```
3437

3538
## Multiple pages
@@ -40,6 +43,7 @@ To launch multiple pages for a browser:
4043
browser := rod.New().MustConnect()
4144
page1 := browser.MustPage("http://a.com")
4245
page2 := browser.MustPage("http://b.com")
46+
fmt.Println(page1, page2)
4347
```
4448

4549
If a browser already has several pages open and you don't have references to them, you can use [Browser.Pages()](https://pkg.go.dev/github.com/go-rod/rod#Browser.Pages) to get a [Pages](https://pkg.go.dev/github.com/go-rod/rod#Pages) struct which is a list of tabs and/or windows with several helpful methods attached, such as [Pages.Find()](https://pkg.go.dev/github.com/go-rod/rod#Pages.Find), [Pages.FindByURL()](https://pkg.go.dev/github.com/go-rod/rod#Pages.FindByURL), [Pages.First()](https://pkg.go.dev/github.com/go-rod/rod#Pages.First), etc. Once you get a reference to the page you want you can use [Page.Activate()](https://pkg.go.dev/github.com/go-rod/rod#Page.Activate) to focus it. If you are clicking a link opens a new page then you can use [Page.WaitOpen](https://pkg.go.dev/github.com/go-rod/rod#Page.WaitOpen) to grab a reference to the new window as soon as it is launched.

context-and-timeout.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ err := rod.Try(func() {
8282
page.Timeout(2 * time.Second).MustNavigate("http://github.com")
8383
})
8484
if errors.Is(err, context.DeadlineExceeded) {
85-
// code for timeout error
85+
fmt.Println("timeout error")
8686
} else if err != nil {
87-
// code for other types of error
87+
fmt.Println("other types of error")
8888
}
8989
```
9090

custom-launch.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ You can use the `launcher.NewUserMode` to launch your regular user browser. Rod
133133

134134
```go
135135
wsURL := launcher.NewUserMode().MustLaunch()
136-
browser := rod.New().ControlURL(wsURL).MustConnect().NoDefaultDevice()
136+
rod.New().ControlURL(wsURL).MustConnect().NoDefaultDevice()
137137
```
138138

139139
Here's a more detailed example: [code example](https://github.com/go-rod/rod/blob/master/lib/examples/use-rod-like-chrome-extension/main.go).

emulation.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,17 @@ If you want to specify the viewport for a specific page, use [Page.SetViewport](
5959
You can use the launch env to set for all pages:
6060

6161
```go
62-
u := launcher.New().Env("TZ=America/New_York").MustConnect()
63-
browser := rod.New().ControlURL(u).MustConnect()
62+
u := launcher.New().Env(append(os.Environ(), "TZ=America/New_York")...).MustLaunch()
63+
rod.New().ControlURL(u).MustConnect()
6464
```
6565

6666
Or you can use [EmulationSetTimezoneOverride](https://pkg.go.dev/github.com/go-rod/rod/lib/proto#EmulationSetTimezoneOverride)
6767
or [EmulationSetLocaleOverride](https://pkg.go.dev/github.com/go-rod/rod/lib/proto#EmulationSetLocaleOverride)
6868
to set for a specific page:
6969

7070
```go
71-
proto.EmulationSetTimezoneOverride{TimezoneID: "America/New_York"}.Call(page)
71+
page := browser.MustPage()
72+
_ = proto.EmulationSetTimezoneOverride{TimezoneID: "America/New_York"}.Call(page)
7273
```
7374

7475
## Permissions
@@ -84,10 +85,11 @@ Use [EmulationSetGeolocationOverride](https://pkg.go.dev/github.com/go-rod/rod/l
8485
Use [EmulationSetEmulatedMedia](https://pkg.go.dev/github.com/go-rod/rod/lib/proto#EmulationSetEmulatedMedia)
8586

8687
```go
87-
proto.EmulationSetEmulatedMedia{
88+
page := browser.MustPage()
89+
_ = proto.EmulationSetEmulatedMedia{
8890
Media: "screen",
8991
Features: []*proto.EmulationMediaFeature{
90-
{"prefers-color-scheme", "dark"},
92+
{Name: "prefers-color-scheme", Value: "dark"},
9193
},
9294
}.Call(page)
9395
```

error-handling.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ Here's the source code of the `MustElement`, as you can see it just calls the `E
1313
several extra lines to panic if err is not `nil`:
1414

1515
```go
16-
func (p *Page) MustElement(selectors ...string) *Element {
17-
el, err := p.Element(selectors...)
18-
if err != nil {
19-
panic(err)
20-
}
16+
// Page ...
17+
type Page rod.Page
18+
19+
// MustElement ...
20+
func (p *Page) MustElement(selector string) *rod.Element {
21+
el, err := (*rod.Page)(p).Element(selector)
22+
if err != nil {
23+
panic(err)
24+
}
2125
return el
2226
}
2327
```
@@ -33,13 +37,11 @@ page := rod.New().MustConnect().MustPage("https://example.com")
3337

3438
el, err := page.Element("a")
3539
if err != nil {
36-
handleError(err)
37-
return
40+
panic(err)
3841
}
3942
html, err := el.HTML()
4043
if err != nil {
41-
handleError(err)
42-
return
44+
panic(err)
4345
}
4446
fmt.Println(html)
4547
```
@@ -53,16 +55,21 @@ page := rod.New().MustConnect().MustPage("https://example.com")
5355
err := rod.Try(func() {
5456
fmt.Println(page.MustElement("a").MustHTML())
5557
})
56-
handleError(err)
58+
panic(err)
5759
```
5860

5961
## Check the error type
6062

6163
We use Go's standard way to check error types, no magic.
6264

63-
The `handleError` in the above code may look like:
65+
Replace the `panic` in the above code with `handleError`:
6466

6567
```go
68+
func main() {
69+
_, err := page.Element("a")
70+
handleError(err)
71+
}
72+
6673
func handleError(err error) {
6774
var evalErr *rod.ErrEval
6875
if errors.Is(err, context.DeadlineExceeded) { // timeout error

input.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To simulate the mouse click an element:
1111
page.MustElement("button").MustClick()
1212

1313
// right click
14-
page.MustElement("button").Click(proto.InputMouseButtonRight)
14+
_ = page.MustElement("button").Click(proto.InputMouseButtonRight)
1515
```
1616

1717
## Text input
@@ -94,18 +94,18 @@ page.MustElement("select").MustSelect("B", "C")
9494
You can also use regex or css selector to select options:
9595

9696
```go
97-
page.MustElement("select").Select([]string{`^B$`}, true, rod.SelectorTypeRegex)
97+
_ = page.MustElement("select").Select([]string{`^B$`}, true, rod.SelectorTypeRegex)
9898

9999
// set false to deselect
100-
page.MustElement("select").Select([]string{`[value="c"]`}, false, rod.SelectorTypeCSSSector)
100+
_ = page.MustElement("select").Select([]string{`[value="c"]`}, false, rod.SelectorTypeCSSSector)
101101
```
102102

103103
## Set files
104104

105105
Use `SetFiles` to set files for the [file input](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file):
106106

107107
```go
108-
pag.MustElement(`[type=file]`).MustSetFiles("a.jpg", "b.pdf")
108+
page.MustElement(`[type=file]`).MustSetFiles("a.jpg", "b.pdf")
109109
```
110110

111111
## Mouse, keyboard, and touch

javascript-runtime.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Now the page can invoke this method on the window object:
5454

5555
```go
5656
hash := page.MustEval(`() => window.md5("test")`).Str()
57+
fmt.Println(hash)
5758
```
5859

5960
Here's another example to get button click event on the page:

network.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ You can throttle the network to simulate and test the slow network effect on you
4747
```go
4848
page.EnableDomain(proto.NetworkEnable{})
4949

50-
proto.NetworkEmulateNetworkConditions{
50+
_ = proto.NetworkEmulateNetworkConditions{
5151
Offline: false,
5252
Latency: 300,
5353
DownloadThroughput: 100,

page-resources/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ You can use the `Page.GetResource` method to get files from here:
77
Such as get an image:
88

99
```go
10-
bin, err := page.GetResource("https://test.com/a.png")
10+
bin, _ := page.GetResource("https://test.com/a.png")
11+
fmt.Println(bin)
1112
```
1213

1314
## Element resource
@@ -17,4 +18,5 @@ Such as for element `<img src="a.jpg">`, you can use code like this to get the `
1718

1819
```go
1920
bin := page.MustElement("img").MustResource()
21+
fmt.Println(bin)
2022
```

selectors/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ func main() {
135135

136136
if page.MustHas(".nav-user-icon-base") {
137137
// print the username after successful login
138-
fmt.Println(*el.MustAttribute("title"))
138+
fmt.Println(page.MustElement(".nav-user-icon-base").MustAttribute("title"))
139139
} else if page.MustHas("[data-cy=sign-in-error]") {
140140
// when wrong username or password
141-
fmt.Println(el.MustText())
141+
fmt.Println(page.MustElement("[data-cy=sign-in-error]").MustText())
142142
}
143143
}
144144
```
@@ -155,7 +155,7 @@ func main() {
155155
// It will keep polling until one selector has found a match
156156
page.Race().Element(".nav-user-icon-base").MustHandle(func(e *rod.Element) {
157157
// print the username after successful login
158-
fmt.Println(*e.MustAttribute("title"))
158+
fmt.Println(e.MustAttribute("title"))
159159
}).Element("[data-cy=sign-in-error]").MustHandle(func(e *rod.Element) {
160160
// when wrong username or password
161161
panic(e.MustText())

0 commit comments

Comments
 (0)