c# - C# 无法使用 WebBrowser 登录
问题描述
我是 C# 的新手。我需要帮助才能登录网页并读取一些数据。
谷歌搜索后,我试图找到下面的代码和其他资源,但在所有情况下,我只能获取登录页面的 html 源,而不能获取其他页面源数据。
- 我需要先遍历主页。
- 然后,我需要遍历“端口状态”并读取一些有用的数据。通知数据存储在帧中。如何从帧中读取数据?
添加更多信息
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);
}
}
}
解决方案
对于登录页面,我会在登录的输入上执行 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。你可以用你的实际密码替换“密码”。
我希望这会有所帮助,如果您需要一些解释,请告诉我。
另请参阅此以使用框架
推荐阅读
- c# - .net c#限制observablecollection中的条目数
- google-apps-script - Google App Script 更新另一个工作表中的单元格值
- objective-c - NSViewController 窗口未调整大小且未关闭
- jsf - 如何使用 Netbeans 和 Primefaces 在 JSF 应用程序中创建一个空页面?
- php - php中的password_verify登录问题
- python - 如何使用 Homebrew 升级到特定的 Python 版本?
- linux - 为什么 Ubuntu 安装带有破坏 autoconf 的版本后缀的 ARM64 编译器?
- prolog - Problog 中的互斥性
- node.js - RangeError:超出最大调用堆栈大小。谁能帮我?
- vba - 使用 Document.Paragraphs.Add() 创建多个 Word 段落