Skip to content

Latest commit

 

History

History
61 lines (42 loc) · 2.34 KB

browsers-pages.md

File metadata and controls

61 lines (42 loc) · 2.34 KB

Browsers & Pages

It's intuitive to use Rod to control multiple browsers or pages at the same time.

Multiple browsers

To launch multiple browsers:

browser1 := rod.New().MustConnect()
browser2 := rod.New().MustConnect()
fmt.Println(browser1, browser2)

All APIs are thread-safe, same works for multiple Go routines.

You can also use incognito mode to launch multiple browsers:

browser1 := rod.New().MustConnect()
browser2 := browser1.MustIncognito()
fmt.Println(browser1, browser2)

Launch browsers with different launch arguments:

browser1 := rod.New().ControlURL(
    launcher.New().Headless(false).MustLaunch(),
).MustConnect()

browser2 := rod.New().ControlURL(
    launcher.New().UserDataDir("path").MustLaunch(),
).MustConnect()
fmt.Println(browser1, browser2)

Multiple pages

To launch multiple pages for a browser:

browser := rod.New().MustConnect()
page1 := browser.MustPage("http://a.com")
page2 := browser.MustPage("http://b.com")
fmt.Println(page1, page2)

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

Page pool

We can use PagePool to help concurrently control and reuse pages.

Check this example

Browser pool

The tests in Rod is a good example of managing a pool of browsers to run tests concurrently. That's why the tests can finish in seconds. Check the code here.