sometimes i open a url but the url is not successful load then the script is deadlock
how to solve the problem
I'm not really sure about your problem. Can you post what you have written so far here?
If you need some kind of timeout behaviour, at the moment chromedp have an action that calls time.Sleep https://godoc.org/github.com/knq/chromedp#Sleep
Note: this is a temporary action, and will likely be marked for deprecation in the future.
Please provide more information by what you mean by 'deadlock'. Thanks!
sorry .the question is the website do not successful open .the element i want to click is not load .so the script cannot continue to run 銆俰t will continue to find the element forever.
i want to the function is that If the time to find the element has not been loaded out is more than a diuration, then it will refresh the page.
@kenshaw by the way, how do I know document is ready or window is loaded? which is similar to this issue.
@kenshaw the question is that if i search the element more than one minute how can i write to let it refresh the page
@kenshaw the question is that if i search the element more than one minute how can i write to let it refresh the page
I haven't had time to finish moving the settings for the timeouts to being stored/read from context.Context, which is where they ultimately should belong. But when that is finished, you would do something like the following:
func ActuallyDoSomething(ctxt context.Context, c *chromedp.CDP) error {
/* ... */
}
func WaitToDoSomething(ctxt context.Context, c *chromedp.CDP, attempts int) error {
timeoutContext, cancel := context.WithTimeout(1*time.Minute)
defer cancel()
var err error
for i := 0; i < attempts; i++ {
err = c.Run(timeoutContext, chromedp.WaitVisible(`#some-element`, chromedp.ByID))
if err == nil {
return ActuallyDoSomething(ctxt, c)
}
err = c.Run(ctxt, chromedp.Reload())
if err != nil {
return err
}
}
return fmt.Errorf("exhausted attempts (%d) waiting for #some-element", attempts)
}
@kenshaw thanks
@kenshaw is it possible to use context.WithTimeout already?
Or maybe there is a way to have some kind of flow control, so that if one thing appeared on the screen do one thing, if another - another?
I also have this problem but it is not the same.the button or label is ready and visible,but the corresponding js code is not loaded successfully.so click is not work.my question is how to wait the page load complete?
I am using chromedp v0.5.1 and I solved as shown below.
func RunWithTimeOut(ctx *context.Context, timeout time.Duration, tasks chromedp.Tasks) chromedp.ActionFunc {
return func(ctx context.Context) error {
timeoutContext, cancel := context.WithTimeout(ctx, timeout * time.Second)
defer cancel()
return tasks.Do(timeoutContext)
}
}
If button not show in 3 seconds browser will be closed.
chromedp.Run(
ctx,
RunWithTimeOut(&ctx, 3, chromedp.Tasks{
chromedp.WaitVisible("button.myButton"),
}),
)
Can I somehow wait for an element without an ID? Like for <h2>NEW</h2>?
Yes. Check the By options here:
https://github.com/chromedp/chromedp/blob/5b511f121cb2fc1e868b8667049d3cff32a6e0b1/query.go#L85-L108
The suggestion is to try the query in Chrome console first.
Most helpful comment
I haven't had time to finish moving the settings for the timeouts to being stored/read from
context.Context, which is where they ultimately should belong. But when that is finished, you would do something like the following: