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

encountered an undefined value #888

Closed
FournyP opened this issue Jul 24, 2021 · 2 comments
Closed

encountered an undefined value #888

FournyP opened this issue Jul 24, 2021 · 2 comments

Comments

@FournyP
Copy link

FournyP commented Jul 24, 2021

What versions are you running?

$ go list -m github.com/chromedp/chromedp
github.com/chromedp/chromedp v0.7.4
$ google-chrome --version
Chromium : 91.0.4472.164
$ go version
go version go1.16.6 windows/amd64

What did you do? Include clear steps.

Hi everyone, I have a function to set some id on select tags.

Here my code :

chromedp.ActionFunc(func(c context.Context) error { // Function to add ids on selects

        var err error
	var selects []*cdp.Node

	chromedp.Nodes("select", &selects, chromedp.ByQueryAll).Do(c)

	var buffer *runtime.RemoteObject

	for index, node := range selects {

		expression := "document.querySelectorAll('select')[" + strconv.Itoa(index) + "].setAttribute('id', '#')"

		if node.ChildNodeCount == 12 { // Month select
			expression = strings.ReplaceAll(expression, "#", "monthSelect")
			err = chromedp.Evaluate(expression, buffer).Do(c)
		} else if node.ChildNodeCount <= 31 && node.ChildNodeCount >= 28 { // Day select
			expression = strings.ReplaceAll(expression, "#", "daySelect")
			err = chromedp.Evaluate(expression, buffer).Do(c)
		} else { // Year select
			expression = strings.ReplaceAll(expression, "#", "yearSelect")
			err = chromedp.Evaluate(expression, buffer).Do(c)
		}

		if err != nil {
			return err
		}
	}

	return err
}),

I use the *runtime.RemoteObject as a buffer to handle the undefined result

However, I meet the error : encountered an undefined value

Maybe I do something wrong

What did you expect to see?

No error, but I want to handle the error if another occur

What did you see instead?

The error : encountered an undefined value

Have a good day, and thanks for your help

@ZekeLu
Copy link
Member

ZekeLu commented Jul 24, 2021

If the result value of chromedp.Evaluate is of type runtime.RemoteObject, the parameter should be of type **runtime.RemoteObject. Here is the change to correct your code:

-err = chromedp.Evaluate(expression, buffer).Do(c)
+err = chromedp.Evaluate(expression, &buffer).Do(c)

What's more, if you don't care about the result value, you can just pass nil to the parameter res (since 0.7.3. see #816).

-err = chromedp.Evaluate(expression, buffer).Do(c)
+err = chromedp.Evaluate(expression, nil).Do(c)

I think the most simple way is to implement the logic with javascript and evaluate the javascript expression with a simple call to chromedp.Evaluate.

expression := `
document.querySelectorAll('select').forEach(s => {
    const childCount = s.children.length;
    const id = childCount === 12 ? 'monthSelect' :
        childCount >= 28 && childCount <= 31 ? 'daySelect' : 'yearSelect'
    s.setAttribute('id', id);
})
`
// ...
	chromedp.Evaluate(expression, nil),

@FournyP
Copy link
Author

FournyP commented Jul 25, 2021

Hello @ZekeLu

It work

Thanks for your reply,

Have a good day

@FournyP FournyP closed this as completed Jul 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants