2016-04-13 46 views
0

我想创建一个程序,可以通过C#登录到网站,但也使用默认浏览器。使用默认浏览器登录到网页c#

目前,它可以在窗体浏览器中正常工作,但我找不到代码以使其适用于实际的浏览器。

任何反馈表示赞赏,

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; 
     } 

    } 

} 

回答

0

尝试www.seleniumhq.org

硒自动化的浏览器。而已!你用这种力量做的事情完全取决于你。主要用于测试目的的自动化网络应用程序 ,但肯定不仅限于此。 无聊的基于Web的管理任务也可以(也应该)也自动化为 。

它支持C#和其他语言。

+0

的代码可能会被推广到了很多人,所以我需要到它的工作原理,以适应这个代码。谢谢你 –

0

可以通过添加COM参考“Microsoft Internet Controls”和“Microsoft HTML Object Library”来自动化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", "[email protected]"); 
}; 

// naviagte to the page 
ie.Navigate("https://en-gb.facebook.com/"); 

// wait indefinitely without blocking the current thread 
new AutoResetEvent(false).WaitOne(); 
相关问题