尝试使用C#登录页面但无法登录,因为每次我点击url时webbrowser都会返回null [英] Trying to login to the page with C# but cant login because webbrowser is returning null everytime i hit the url

查看:61
本文介绍了尝试使用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);
            }
           
        }

推荐答案

请不要使用 Thread.Sleep(int)因为无法保证页面将被加载并准备好任意时间限制。

使用WebBrowser控件公开的事件,特别是 WebBrowser.DocumentCompleted Event [ ^ ]。



除此之外,你必须做一些认真的调试。单步执行代码将帮助您了解每一步到底发生了什么。



问候,

- Manfred
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


除了''解决方案1'':

您正在搜索错误的输入元素。首先,你不需要迭代元素,你可以通过id搜索:

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:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆