Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void DownloadLabelingListFile()
        {
            try
            {
                // Part 1: Use WebBrowser control to load web page
                WebBrowser wb = new WebBrowser();
                wb.Navigate(@"https://gateway.usps.com/bcg/login.htm");

                System.Threading.Thread.Sleep(2000);
                // Delay 2 seconds to render login page

                // Part 2: Automatically input username and password
                HtmlElementCollection theElementCollection = default(HtmlElementCollection);
                theElementCollection = wb.Document.GetElementsByTagName("input");

                //UserName
                foreach (HtmlElement curElement in theElementCollection)
                {
                    string usernameControl = curElement.GetAttribute("login_name").ToString();

                    if (usernameControl == "UserNameTextBox")
                        curElement.SetAttribute("Value", "***");

                }

                //Password
                foreach (HtmlElement curElement in theElementCollection)
                {
                    string passwordControl = curElement.GetAttribute("user_password").ToString();

                    if (passwordControl == "PasswordTextBox")
                        curElement.SetAttribute("Value", "***");

                }


                // Part 3: Automatically clck that Login button
                theElementCollection = wb.Document.GetElementsByTagName("input");

                foreach (HtmlElement curElement in theElementCollection)
                {
                    if (curElement.GetAttribute("value").Equals("Submit"))
                    {
                        curElement.InvokeMember("click");
                        // javascript has a click method for we need to invoke on button and hyperlink elements.
                    }

                } 
                #endregion
            }
            catch (ThreadAbortException)
            {

            }
            catch (HttpListenerException exc)
            {
                Logger.Err(Logger.LogFileType.ServiceReference, "DownloadLabelingListFile::CDownloader", exc);
            }
           
        }
Posted
Updated 13-Mar-13 4:10am
v2
Comments
[no name] 13-Mar-13 10:07am    
Maybe you can format your code so that it is readable. While you are at it, please add some sort of a question to your code dump.
Manfred Rudolf Bihy 13-Mar-13 10:08am    
Returning "null" where exactly in this, not even formatted, code dump.
Why are you waiting are you using Thread.Sleep(2000)? That makes absolutely no sense, as the page may not even have been rendered completely in two seconds.
Use the events exposed by the WebBrowser control as DocumentCompleted for instance.

Please don't use Thread.Sleep(int) as there is no guarantee, that a page will be loaded and ready withing any arbitrary time limit.
Use the events exposed by the WebBrowser control, especially the WebBrowser.DocumentCompleted Event[^].

Aside from that, you'll have to do some serious debugging. Stepping through the code will help you understand what exactly is happening at each step.

Regards,
— Manfred
 
Share this answer
 
v2
Comments
AddyRuno 13-Mar-13 10:21am    
Its still standing with the same condition :(
In addition to 'solution 1':
You're searching for the input elements wrong. First of all you don't need to iterate over elements, you can search by ids:
C#
private void DownloadLabelingListFile()
{
    try
    {
        // Part 1: Use WebBrowser control to load web page
        WebBrowser wb = new WebBrowser();
        webBrowserForPrinting.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ProcessDocument);
        
        wb.Navigate(@"https://gateway.usps.com/bcg/login.htm");
    }
    catch (ThreadAbortException)
    {

    }
    catch (HttpListenerException exc)
    {
        Logger.Err(Logger.LogFileType.ServiceReference, "DownloadLabelingListFile::CDownloader", exc);
    }
   
}

private void ProcessDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // Print the document now that it is fully loaded.
    WebBrowser wb = (WebBrowser)sender;

   //UserName
    HtmlElement userInputElement = wb.Document.GetElementById("login_name");
    userInputElement.SetAttribute("Value", "***");
    
    //UserName
    HtmlElement passInputElement = wb.Document.GetElementById("user_password");
    passInputElement.SetAttribute("Value", "***");

    //  Add more processing here
}


I don't recommend using WebBrowser control for this kind of stuff, unless you really need the ability to execute javaScript inside page.
It would be much easier to do this with System.Net.WebClient or System.Net.HttpWebRequest classes.
 
Share this answer
 
v2
Comments
AddyRuno 14-Mar-13 1:20am    
//But problem is when it comes to here wb.Document is Null and how could i find the element from the null web document?

theElementCollection = wb.Document.GetElementsByTagName("input");
AddyRuno 14-Mar-13 6:49am    
any good solution?

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