Greetings Rick.
I stumbled upon your article "Chromium WebView2 Control and .NET to JavaScript Interop - Part 2" and hope you can assist me.
We are working with servers from various manufacturers. In order to access the onboard management systems, I have written a small program with which I can access these using an embedded web control. The program simplifies the access by storing the IP address as well as the login data.
Previously I used the WebBrowser control, which worked for the older server models. The newer models have problems with the old IE interface, so now I am using the WebView2 control.
In my program the user enters the login data in two text boxes and presses a button to "login". With the button press, I insert the data in the various fields on the form and simulate a click event on the relevant button on the form.
For some servers this works fine, for others not a all. Even though the username and password does get entered on the webform, it is somehow not recognised. Only after I retype the exact same info in the field does the form accept the input.
Here a piece of code that I use:
var script = "document.getElementById('" + username + "').value = '" + userNameEntry + "';";
script += "document.getElementById('" + password + "').value = '" + passwordEntry + "';";
script += "document.getElementById('" + submit + "').click();";
await WebViewServerBrowser.ExecuteScriptAsync(script);
'username', 'password' and 'submit' are the relevant IDs as per HTML source. Since the text gets inserted in the correct field, I can be sure that the ElementIDs are correct.
Do you have any idea what is wrong? Or is my approach wrong?
Thanks for your help in this matter.
Kinde Regards Hartmut
It could be a timing issue.
Make sure:
- You wait for the document to become active
- Use
setTimeout()for the button click to allow the text to be 'applied'
Also some sites require focus - you might want to set focus to the first input box, then submit.
+++ Rick ---