C#无法使用WebBrowser登录

时间:2018-05-30 08:32:47

标签: c# login webbrowser-control

我是C#的新手。我需要帮助才能登录网页并阅读一些数据。

谷歌搜索后,我试图找到下面的代码和其他资源,但在所有情况下,我只能获取登录页面的html源,但不能获取其他页面的源数据。

  1. 我需要首先遍历主页。
  2. 然后,我需要遍历 到“端口状态”并读取一些有用的数据。通知数据存储在 帧。如何从框架中读取数据?
  3. 添加更多信息

    1)view-source:http://192.168.0.239/homepage.html,调用脚本如下所示

    getSubTree( '管理');

    2)上述调用命中了java脚本文件(http://192.168.0.239/frame.js

    中的内容
        case "Management":
           str += OneNodeLink("lv1", "Switch Information", "/iss/specific/sysInfo.html?Gambit="+GAMBIT);
           str += OneNodeLink("lv1", "Port Status", "/iss/specific/port_settings.html?Gambit="+GAMBIT);
    

    document.getElementById(“treeFrame”)。innerHTML = str;

    3)上面的代码执行此文件“view-source:http://192.168.0.239/iss/specific/port_settings.html?Gambit=pisfgagehesfhjikojngqcabdfkjeeffmpkhfckm”并获取“端口状态”

    我的要求是读取来自帧数据的“端口状态”。希望我能说清楚。如果您需要更多信息,请与我们联系。

    Link有屏幕截图和html源文件:https://www.dropbox.com/sh/oml3tk75tf1lu5c/AADuGtbZci3gnyOQ2AE8IYwua?dl=0

    提前多多感谢

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace WebBrowserWithoutAForm
    {
        class Program
        {
            private static bool completed = false;
            private static WebBrowser wb;
            [STAThread]
            static void Main(string[] args)
            {
                wb = new WebBrowser();
                wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
    
                string postData = string.Format("LoginPassword={0}&login=Login", "password");
                ASCIIEncoding enc = new ASCIIEncoding();
                wb.Navigate("http://192.168.0.239", "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");            
    
                //wb.Navigate("http://192.168.0.239");
    
                while (!completed)
                {
                    Application.DoEvents();
                    Thread.Sleep(100);
                }
                Console.Write("\n\nDone with it!\n\n");
                Console.ReadLine();
            }
    
            static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                //Console.WriteLine(wb.Document.Body.InnerHtml);
                completed = true;
    
                Thread.Sleep(1000);
    
               //*******HERE I NEED TO TRAVERSE TO THE HOME PAGE AND GET ITS SOURCE ******
                wb.Navigate("http://192.168.0.239/homepage.html");
    
                Console.WriteLine(wb.DocumentText);
            }
        }
    }
    

2 个答案:

答案 0 :(得分:0)

我建议您使用selenium - my sample code of using Selenium to login facebook

我不会为你编码,但你可以复制我的部分代码。

答案 1 :(得分:0)

对于登录页面,我会在登录的输入上执行GetElementById,然后设置value属性。然后对于登录按钮,如果它实际上是一个按钮,我会触发click()事件,否则如果它是一个表单,那么我会做一个提交。

对于端口状态,您将再次需要知道主页的html并对其执行GetElementById,然后触发click()事件。

为这些单词添加一些代码,它看起来像这样:

        var portStatus = this.webBrowser1.Document.GetElementById("portStatus");
        portStatus.InvokeMember("click");

        //this should give you access to DOM of the first frame on the page.
        //if you have more than 1 then you will need to know which one.
        var frameDoc= this.webBrowser1.Document.Window.Frames[0].Document;           
        //or
        var frameDoc= this.webBrowser1.Document.Window.Frames["iframeid"].Document;;

        var login = this.webBrowser1.Document.GetElementById("Login");
        login.SetAttribute("Value", "password");

        var loginButton = this.webBrowser1.Document.GetElementById("LoginButton");
        loginButton.InvokeMember("click");

        //or if the login button is a form then submit the form
        HtmlElement form = webBrowser1.Document.GetElementById("FormID");
        if (form != null)
            form.InvokeMember("submit");

由于您没有提供主页的HTML,因此此处使用的ID必须由页面的实际ID替换。你可以用你的实际密码替换“密码”。

我希望这有帮助,如果您需要一些解释,请告诉我。

另请参阅this了解框架