无法在C#中使用Webbrowser以编程方式登录网站

时间:2018-08-29 04:58:15

标签: c# login htmlelements

我尝试使用c#中的webbrowser登录到网站(“ https://mytel.com.mm/”)。 无法登录。下面是代码。

/// <summary>
    /// The main entry point for the application.
    /// </summary>
    private static WebBrowser wb;
    [STAThread]
    static void Main()
    {
        wb = new WebBrowser();
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        wb.Navigate("https://mytel.com.mm/");

        Application.Run();
    }
    static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        string Logurl = wb.Url.AbsoluteUri;
        if (!wb.Url.ToString().Contains("myviettel"))
        {
            var InputElements = wb.Document.GetElementsByTagName("input");
            foreach (HtmlElement i in InputElements)
            {
                if (i.GetAttribute("ng-model").Equals("loginForm.username"))
                {
                    i.Focus();
                    i.InnerText = "xxxxxxxxx";
                }
                if (i.GetAttribute("ng-model").Equals("loginForm.password"))
                {
                    i.Focus();
                    i.InnerText = "xxxxxxxxx";
                    i.RemoveFocus();
                }
            }

            var buttonElements = wb.Document.GetElementsByTagName("button");
            foreach (HtmlElement b in buttonElements)
            {
                if (b.GetAttribute("className").Equals("btn btn-nat-input-popup-bottom margin-bottom-10 ng-binding"))
                {
                    b.InvokeMember("click");
                }
            }
        }
        else
        {
            //logged in
        }
    }

成功登录后,URL为“ https://www.mytel.com.mm/myviettel”。
而且我认为Logurl变量应包含(“ https://www.mytel.com.mm/myviettel”)。

谢谢

1 个答案:

答案 0 :(得分:0)

为了得到答案,我使用了Selenium Chromedriver服务,该服务可以通过编程成功登录。

相关问题