2013-03-16 61 views
0

我一直在嘗試使用GetElementByTag/GetElemenByName方法自動登錄到使用WebBrowser控件的網站,但沒有多大成功。 (WFA - C#)如何使用Windows窗體登錄基於JavaScript的網站?

我相信主要原因是該網站是在JavaScript中。

我做了一些研究,發現兩種方法:

  1. 通過模仿網站的登錄表單,我可以使用一些所謂的POST。 (或者類似的東西)
  2. 注入的JavaScript輸入到所有的輸入字段網站

我不知道如何解決這個問題,因爲我完全沒有用JavaScript或任何經驗基於網絡的編程,我可以真正使用任何建議或解決方案來處理這個問題。

/**編輯1 感謝您的快速響應。 香港專業教育學院試圖用你的代碼,仍然面臨着同樣的異常拋出..

這裏是代碼: (我修剪它有點)

public WorkerClass(string url) 
    { 
    webBrowser1 = new WebBrowser(); 
    webBrowser1.Navigate(url); 
    webBrowser1.Document.GetElementById("c_Username").InnerText = "?????"; 
    webBrowser1.Document.GetElementById("c_Password").InnerText = "?????"; 
    } 

,我得到一個「System.NullReferenceExeption」上上面的用戶名行。

該網站即時嘗試訪問是 - 「http://www.gool.co.il」......也許我的做法是錯誤的?!

回答

1

您可以使用網頁瀏覽器控制,只需在您的目標頁面上找到元素ID並填充它們即可。

我寫一個簡單的給你:

public Form1() 
     { 
      InitializeComponent(); 
      //navigate to you destination 
      webBrowser1.Navigate("https://www.certiport.com/portal/SSL/Login.aspx"); 
     } 
     bool is_sec_page = false; 
     private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      if (!is_sec_page) 
      { 
       //get page element with id 
       webBrowser1.Document.GetElementById("c_Username").InnerText = "username"; 
       webBrowser1.Document.GetElementById("c_Password").InnerText = "pass"; 
       //login in to account(fire a login button promagatelly) 
       webBrowser1.Document.GetElementById("c_LoginBtn_c_CommandBtn").InvokeMember("click"); 
       is_sec_page = true; 
      } 
      //secound page(if correctly aotanticate 
      else 
      { 
       //intract with sec page elements with theire ids 
      } 

     } 
+0

這是相當不錯的:),簡單到如此地步,+ 1吧 – 2013-03-16 19:26:06