首页 > 解决方案 > C# 无法使用 WebBrowser 登录

问题描述

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

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

  1. 我需要先遍历主页。
  2. 然后,我需要遍历“端口状态”并读取一些有用的数据。通知数据存储在帧中。如何从帧中读取数据?

添加更多信息

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 ”得到“端口状态”

我的要求是读取从帧数据中接收到的“端口状态”。希望我能说清楚。如果您需要更多信息来帮助,请告诉我。

链接有截图和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);
        }
    }
}

标签: c#loginwebbrowser-control

解决方案


对于登录页面,我会在登录的输入上执行 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。你可以用你的实际密码替换“密码”。

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

另请参阅以使用框架


推荐阅读