Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Fix underlying frame.waitForSelector #1534

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,16 @@ func (f *Frame) waitForSelectorRetry(
return nil, err
}

// waitForSelector will wait for the given selector to reach a defined state in
// opts.
//
// It will auto retry on certain errors until the retryCount is below 0. The
// retry workaround is needed since the underlying DOM can change when the
// wait action is performed during a navigation.
func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptions) (_ *ElementHandle, rerr error) {
f.log.Debugf("Frame:waitForSelector", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

document, err := f.document()
if err != nil {
return nil, err
}

handle, err := document.waitForSelector(f.ctx, selector, opts)
handle, err := f.waitFor(selector, opts, 20)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -564,23 +565,23 @@ func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptio
return handle, nil
}

func (f *Frame) waitFor(selector string, opts *FrameWaitForSelectorOptions, retryCount int) error {
func (f *Frame) waitFor(selector string, opts *FrameWaitForSelectorOptions, retryCount int) (_ *ElementHandle, rerr error) {
f.log.Debugf("Frame:waitFor", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

retryCount--
if retryCount < 0 {
return errors.New("waitFor retry threshold reached")
return nil, errors.New("waitFor retry threshold reached")
}

document, err := f.document()
if err != nil {
if strings.Contains(err.Error(), "Cannot find context with specified id") {
return f.waitFor(selector, opts, retryCount)
}
return err
return nil, err
}

_, err = document.waitForSelector(f.ctx, selector, opts)
handle, err := document.waitForSelector(f.ctx, selector, opts)
if err != nil {
if strings.Contains(err.Error(), "Inspected target navigated or closed") {
return f.waitFor(selector, opts, retryCount)
Expand All @@ -593,7 +594,7 @@ func (f *Frame) waitFor(selector string, opts *FrameWaitForSelectorOptions, retr
}
}

return err
return handle, err
}

// ChildFrames returns a list of child frames.
Expand Down
3 changes: 2 additions & 1 deletion common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@ func (l *Locator) WaitFor(opts sobek.Value) error {

func (l *Locator) waitFor(opts *FrameWaitForSelectorOptions) error {
opts.Strict = true
return l.frame.waitFor(l.selector, opts, 20)
_, err := l.frame.waitFor(l.selector, opts, 20)
return err
}

// DefaultTimeout returns the default timeout for the locator.
Expand Down
Loading