Skip to content
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

element.page 中的timeout context 怎么cancel ? #842

Closed
sqzxcv opened this issue Mar 21, 2023 · 4 comments
Closed

element.page 中的timeout context 怎么cancel ? #842

sqzxcv opened this issue Mar 21, 2023 · 4 comments
Labels
bug When you are sure about it's a bug

Comments

@sqzxcv
Copy link

sqzxcv commented Mar 21, 2023

Rod Version: v0.112.6

The code to demonstrate your question

  1. 下面是测试代码:

     browser := rod.New().ControlURL(launcher.New().Headless(false).MustLaunch()).MustConnect().SlowMotion(time.Duration(2))
    
     page := browser.MustPage("https://www.netflix.com/login")
     page.WaitLoad()
    
     // 超时时间10秒
     el, err := page.Timeout(time.Second * 10).Element("#id_userLoginId")
    
     if err != nil {
         return
     }
     el = el.CancelTimeout()
     // 超时时间 100分钟
     err = el.Timeout(time.Minute*100).Click(proto.InputMouseButtonLeft, 1)
    
     if err != nil {
         return
     }

上面这段代码, 从page 获取 到element时, element携带的timeoutContext, 可以用CancelTimeout 清楚, 但是element中的page成员timeContext并没有被清除, 导致下面 执行Click时, 出现 cancel Context 错误.
具体跟踪click代码运行, 发现在下面这地方, 调用了element.page, 由于page.Context的超时时间没有被清除, 从而任务被取消.

func (el *Element) Interactable() (pt *proto.Point, err error) {
        

	elAtPoint, err := el.page.ElementFromPoint(
		int(pt.X)+scroll.Value.Get("x").Int(),
		int(pt.Y)+scroll.Value.Get("y").Int(),
	)
	if err != nil {
		if errors.Is(err, cdp.ErrNodeNotFoundAtPos) {
			err = &ErrInvisibleShape{el}
		}
		return
	}


}

上面是element 中的一个函数, 这个函数在click中被调用

What you got

由于element.page 超时, 倒是click返回错误: cancel context

What you expected to see

element.click 能正常执行, 除非超时

@sqzxcv sqzxcv added the question Questions related to rod label Mar 21, 2023
@rod-robot
Copy link

Please fix the format of your markdown:

1:22 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]
26 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"]
26 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "```"]
47 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"]
50 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]
59 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]
60 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 3]

generated by check-issue

@ysmood ysmood added bug When you are sure about it's a bug and removed question Questions related to rod labels Mar 21, 2023
@ysmood ysmood closed this as completed in c448d2f Mar 21, 2023
@sqzxcv
Copy link
Author

sqzxcv commented Mar 21, 2023

@ysmood 这个地方也要修改一下
#843 (comment)

// ElementByJS returns the element from the return value of the js
func (el *Element) ElementByJS(opts *EvalOptions) (*Element, error) {
	e, err := el.page.Sleeper(NotFoundSleeper).ElementByJS(opts.This(el.Object))
	if err != nil {
		return nil, err
	}
	return e.Sleeper(el.sleeper), nil
}

这里也有问题 ElementByJS里面用的不是element的Context, 而是page的Context

@ysmood
Copy link
Collaborator

ysmood commented Mar 21, 2023

确实,但是你的 PR 能清理下吗?

@sqzxcv
Copy link
Author

sqzxcv commented Mar 21, 2023

确实,但是你的 PR 能清理下吗?

那个好像是自动格式化的, 不太好清...就一行代码,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug When you are sure about it's a bug
Projects
None yet
Development

No branches or pull requests

3 participants