C# 使用默认浏览器c登录网页#
我正在尝试创建一个程序,可以通过C#登录到网站,但也可以使用默认浏览器 目前,它可以很好地与表单浏览器配合使用,但我找不到使其适应实际浏览器的代码 如有任何反馈,我们将不胜感激C# 使用默认浏览器c登录网页#,c#,browser,login,C#,Browser,Login,我正在尝试创建一个程序,可以通过C#登录到网站,但也可以使用默认浏览器 目前,它可以很好地与表单浏览器配合使用,但我找不到使其适应实际浏览器的代码 如有任何反馈,我们将不胜感激 using System; using System.Windows.Forms; using System.Diagnostics; namespace PortalLogin2 { public partial class Form1 : Form { bool mHooked;
using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace PortalLogin2
{
public partial class Form1 : Form
{
bool mHooked;
public Form1()
{
InitializeComponent();
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
private void button1_Click(object sender, EventArgs e)
{
string input = "https://en-gb.facebook.com/";
Process.Start(input);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (mHooked) return;
HtmlDocument doc = webBrowser1.Document;
HtmlElement username = doc.GetElementById("email");
HtmlElement password = doc.GetElementById("pass");
HtmlElement submit = doc.GetElementById("u_0_");
string txtUser = "insert username here";
string txtPass = "insert password here";
doc.GetElementById("email").InnerText = txtUser.ToString();
doc.GetElementById("pass").InnerText = txtPass.ToString();
submit.InvokeMember("click");
mHooked = true;
}
}
}
试一试
Selenium自动化浏览器。就这样!你用这种力量所做的是
完全由你决定。它主要用于自动化web应用程序
用于测试目的,但肯定不仅仅限于此。
枯燥的基于web的管理任务也可能(而且应该!)被忽略
自动化也是如此
它支持C#和其他语言。通过添加COM引用“Microsoft Internet控件”和“Microsoft HTML对象库”,可以自动化Internet Explorer 下面是一个在Facebook上填写“电子邮件”字段的工作示例:
var ie = new SHDocVw.InternetExplorer();
ie.Visible = true;
// once the page is loaded
ie.DocumentComplete += (object pDisp, ref object URL) => {
// get the document
mshtml.HTMLDocument doc = (mshtml.HTMLDocument)(object)ie.Document;
// set the email field
mshtml.IHTMLElement email = doc.getElementById("email");
email.setAttribute("value", "na@na.na");
};
// naviagte to the page
ie.Navigate("https://en-gb.facebook.com/");
// wait indefinitely without blocking the current thread
new AutoResetEvent(false).WaitOne();
这段代码可能会推广到很多人,所以我需要修改这段代码,直到它起作用为止。谢谢你