本文介绍了尝试使用C#登录页面但无法登录,因为每次我点击url时webbrowser都会返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
            }
           
        }

推荐答案


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
}





除非你真的需要能够在页面内执行javaScript,否则我不建议使用WebBrowser控件。

使用 System.Net.WebClient System.Net.HttpWebRequest class。


这篇关于尝试使用C#登录页面但无法登录,因为每次我点击url时webbrowser都会返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 06:02