question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked TimonYang-MSFT commented

C# How to use web browser control to automate login to a site

I was trying to login to a site programmatically. i am loading site into web browser control. i tried this code but it did not worked.

here is login page url login page url

Sorry could not share actual credentials.

 HtmlElement username = null;
 HtmlElement password = null;
 HtmlElement submit = null;
    
 foreach (HtmlElement divcontainer in webBrowser1.Document.GetElementsByTagName("div"))
 {
     if (divcontainer.GetAttribute("className") == "text-input")
     {
         foreach (HtmlElement child in divcontainer.Children)
         {
             if (divcontainer.GetAttribute("className") == "js-email-input")
             {
                 username = divcontainer;
             }
    
             if (divcontainer.GetAttribute("className") == "password js-password-input")
             {
                 password = divcontainer;
             }
         }
     }
    
     if (divcontainer.GetAttribute("className") == "sign-in")
     {
         foreach (HtmlElement child in divcontainer.Children)
         {
             if (divcontainer.GetAttribute("className") == "solid-button basic-login-submit")
             {
                 submit = divcontainer;
             }
         }
     }
 }
    
 if (username != null && password != null && submit != null)
 {
     username.SetAttribute("value", "anon@user ");
     password.SetAttribute("value", "test11");
     submit.InvokeMember("click");
 }

please some one guide me what to change in my code to automate login. where is wrong in my code. Thanks.




dotnet-csharp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@TZacks-2728
I can't get any elements using this code.
Could it be due to JavaScript?
Do you have any unshown code to do it?

0 Votes 0 ·

@TZacks-2728
Do you still meet this issue? If so, could you please share more information so that we can continue to analyze the problem?

0 Votes 0 ·

yes issue resolved but now site not allowing me to post the sample code.

0 Votes 0 ·
Show more comments

0 Answers