Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
The problem is that when I type the password in textbox2, the space on the web page does not fill (The first one works great)

C#
webView21.ExecuteScriptAsync("document.getElementById('username').value=" + '\'' + textBox1.Text + '\'');
webView21.ExecuteScriptAsync("document.getElementById('password').value=" + '\'' + textBox2.Text + '\'');


What I have tried:

C#
webView21.ExecuteScriptAsync("document.getElementById('password').value=" + '\'' + textBox2.Text + '\'');
Posted
Updated 16-Dec-23 6:19am
v6
Comments
Richard MacCutchan 16-Dec-23 5:36am    
Please use the Improve question link above, and add complete details of what is not working.
Matrix Alsaad 16-Dec-23 11:08am    
webView21.ExecuteScriptAsync("document.getElementById('username').value=" + '\'' + textBox1.Text + '\'');
webView21.ExecuteScriptAsync("document.getElementById('password').value=" + '\'' + textBox2.Text + '\'');
Richard MacCutchan 16-Dec-23 11:26am    
If you use async methods then you also need to use await in order to capture the results.

1 solution

Here's hoping your users never try typing a ' character in their password!

You need to properly escape the strings that you're passing into the ExecuteScriptAsync method. For example:
C#
string encodedUserName = System.Text.Json.JsonSerializer.Serialize(textBox1.Text);
string encodedPassword = System.Text.Json.JsnoSerializer.Serialize(textBox2.Text);
string scriptToExecute = $"document.getElementById('username').value = ${encodedUsername}; document.getElementById('password').value = ${encodedPassword};";
webView21.ExecuteScriptAsync(scriptToExecute);

NB: Do yourself a favour and start giving your controls meaningful names, rather than accepting the defaults suggested by the Visual Studio designer. You might remember which of the 21+ WebView controls to use for login today, but when you come back to this code in a few months, you won't have a clue.
 
Share this answer
 
Comments
Matrix Alsaad 19-Dec-23 10:29am    
Error System.Text.Json
Matrix Alsaad 19-Dec-23 10:37am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900