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

WebView2 ExecuteScriptAsync fails when script is executed a second time #4124

Closed
HaavardL opened this issue Nov 3, 2023 · 5 comments
Closed
Assignees

Comments

@HaavardL
Copy link

HaavardL commented Nov 3, 2023

What happened?

Microsoft.Web.WebView2 1.0.2164-prerelease

Running the script once is OK.
Running the script a second time results in an exception.
Is this an error or a limitation?
Is there a way to "clean up" between script executions?

Script:
let clickButton = function (doc, buttonId) {
let button = doc.getElementById(buttonId);
if (button != null) {
button.click();
}
};
const nextButtonId = "someButtonId";
clickButton(document, nextButtonId);
//...More script code...

Exception from ExecuteScriptWithResultAsync:
{
"columnNumber": 0,
"exception": {
"className": "SyntaxError",
"description": "SyntaxError: Identifier 'clickButton' has already been declared",
"objectId": "-8654842674382291014.2.2",
"subtype": "error",
"type": "object"
},
"exceptionId": 1,
"lineNumber": 0,
"scriptId": "61",
"text": "Uncaught"
}

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

118.0.2088.76

SDK Version

1.0.2164.0

Framework

WPF

Operating System

Windows 10

OS Version

19045.3636

Repro steps

Execute script twice

Regression

Don't know

Last working version (if regression)

No response

@HaavardL HaavardL added the bug Something isn't working label Nov 3, 2023
@vbryh-msft
Copy link
Contributor

Hi, your issue is with JS, not ExecuteScript API - you are redeclaring clickButton. May try to do it in 2 calls - declare in first and do click in second. Then ExecuteScript only for clickling second time.

@HaavardL
Copy link
Author

HaavardL commented Nov 3, 2023

Thanks for the response, but I don't understand what you mean. I am a novice to JS so you probably have a point. I tried to declare clickButton at the top of the script an use it further down.
The essence of the problem is that the script is working fine if I execute it one time, but fails if I execute it one more time.

@vbryh-msft
Copy link
Contributor

I was thinking

script1 = "let clickButton = function (doc, buttonId) {
let button = doc.getElementById(buttonId);
if (button != null) {
button.click();
}
};";

script2 = "clickButton(document, "someButtonId");";

ExcuteScript(script1); // only once
ExcuteScript(script2); // can do multiple  - whenever you need to press the button

Hope it make sense

@HaavardL
Copy link
Author

HaavardL commented Nov 3, 2023

Thanks again. I will try it out tomorrow.

@HaavardL
Copy link
Author

HaavardL commented Nov 4, 2023

Fantastic! It worked. I've never seen a tutorial/example that utilizes this pattern. Thanks again!

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