-
-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Javascript execution on unfocused Chrome tabs? #830
Comments
Please fix the format of your markdown: 35 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"] generated by check-issue |
There is a way! https://sqa.stackexchange.com/a/32355 This forces the page to be hidden or visible. Look, it is visible but I set it to hidden! |
Okay it seems like I cant execute package main
import (
"fmt"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"sync"
)
func main() {
launch := launcher.New().Leakless(false).MustLaunch()
browser := rod.New().ControlURL(launch).MustConnect()
var wg = new(sync.WaitGroup)
wg.Add(5)
go tester(browser, wg)
go tester(browser, wg)
go tester(browser, wg)
go tester(browser, wg)
go tester(browser, wg)
wg.Wait()
}
func tester(browser *rod.Browser, wg *sync.WaitGroup) {
defer wg.Done()
page := browser.MustPage("https://www.wikipedia.org/")
page.MustWaitLoad()
_, err := proto.RuntimeEvaluate{
IncludeCommandLineAPI: true,
Expression: `Object.defineProperty(document, 'visibilityState', {value: 'visible', writable: true});
Object.defineProperty(document, 'hidden', {value: false, writable: true});
document.dispatchEvent(new Event("visibilitychange"));`,
}.Call(page)
if err != nil {
panic(err)
}
result, err := proto.RuntimeEvaluate{
IncludeCommandLineAPI: true,
Expression: `document.visibilityState`,
}.Call(page)
if err != nil {
panic(err)
}
fmt.Println(result.Result.Value)
elem := page.MustElement("#js-link-box-en strong")
elem.MustClick()
} soooooooo, dunno I am out of ideas. |
Yeah, javascript execution works, I clicked the English with JS, but 2023-03-09.23-49-45.mp4package main
import (
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"sync"
)
func main() {
launch := launcher.New().Leakless(false).MustLaunch()
browser := rod.New().ControlURL(launch).MustConnect()
var wg = new(sync.WaitGroup)
wg.Add(5)
go tester(browser, wg)
go tester(browser, wg)
go tester(browser, wg)
go tester(browser, wg)
go tester(browser, wg)
wg.Wait()
}
func tester(browser *rod.Browser, wg *sync.WaitGroup) {
defer wg.Done()
page := browser.MustPage("https://www.wikipedia.org/")
page.MustWaitLoad()
_, err := proto.RuntimeEvaluate{
IncludeCommandLineAPI: true,
Expression: `Object.defineProperty(document, 'visibilityState', {value: 'visible', writable: true});
Object.defineProperty(document, 'hidden', {value: false, writable: true});
document.dispatchEvent(new Event("visibilitychange"));`,
}.Call(page)
if err != nil {
panic(err)
}
_, err = proto.RuntimeEvaluate{
IncludeCommandLineAPI: true,
Expression: `document.visibilityState`,
}.Call(page)
if err != nil {
panic(err)
}
_, err = proto.RuntimeEvaluate{
IncludeCommandLineAPI: true,
Expression: `document.querySelector("#js-link-box-en strong").click()`,
}.Call(page)
if err != nil {
panic(err)
}
} |
I think it's a limitation of Chromium. I usually run multiple browsers to do similar thing, not multiple pages on a single browser. Rod has browser pool helper to do it: Lines 119 to 126 in 3a88bf4
Lines 442 to 450 in 3a88bf4
|
I mean, yeah separate browsers are less painful and more maintenable to use. I wondered this "single browser more tabs" for scalability, "more browser less tabs" eats far more CPU and memory as far as I seen. I will reach out to you if I can think a non-spaghetti way for "single browser more tabs" approach, for now I will stick with BrowserPool. thanks! |
Rod Version: v0.112.6
First watch this to understand what the heck I am trying to say:
2023-03-09.23-10-53.mp4
This code is trying to achieve click the English button at https://www.wikipedia.org/, in 5 separate tabs. BUUUUUT, unfortunately, I cannot do that.
As you see in the video, MustClick executes when I focus on the tabs. In the background, all of them are idle, except the currently focused one.
I dwindled around a bit, and found this:
https://vanilla.aslushnikov.com/?Page.setWebLifecycleState
from:
puppeteer/puppeteer#3339 (comment)
So uhhhhh, these two might be totally unrelevant, but can I achieve something like this with Go Rod? Like, set the lifecycle state to active before executing MustClick, and boom, now I can click the English button in 5 separate tabs.
sorry if this is a too dumb request, it is 1 AM and my sanity is slowly leaking.
The text was updated successfully, but these errors were encountered: