2017-03-05 53 views
0

我想創建一個功能,將自動登錄到某個網站的某人。C#登錄到網站沒有ID

frm.Login和frm.Password存在,因爲我從其他表單導入字符串。那種形式是他們輸入他們的憑證的地方。

免責聲明: 我沒有訪問此站點!

的HTML代碼

<input type="email" required="" size="21" name="email"> //email 
<input type="password" required="" name="password" size="21"> //password 
<input type="submit" name="submit" value="Sign in"> //button to sign in. 

我嘗試

Form3 frm = new Form3(); 
frm.ShowDialog(); 
webBrowser1.Navigate("examplesite.php"); 

webBrowser1.Document.GetElementsByTagName("input") 
      .GetElementsByName("email")[0] 
      .SetAttribute("Value", frm.Login); 

webBrowser1.Document.GetElementsByTagName("input") 
      .GetElementsByName("password")[0] 
      .SetAttribute("Value", frm.Password); 

ClickButton("submit"); 

守則ClickButton

public void ClickButton(string type) 
{ 
    var button = webBrowser1.Document.GetElementsByTagName("submit") 
      .Cast<HtmlElement>() 
      .FirstOrDefault(m => m.GetAttribute("type") == type); 
    if (button != null) 
     button.InvokeMember("click"); 
} 

Error: An unhandled exception of type 'System.NullReferenceException' occurred in Test.exe Additional information: Object reference not set to an instance of an object.

Line that gives error: webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("email")[0].SetAttribute("Value", frm.Login);

+2

你的問題是什麼? – SLaks

+0

如何將我的winforms應用程序中的字符串傳遞到網站的登錄名和密碼文本框中... –

+1

一旦您導航到該網站,您需要等待網站完全加載。現在,您立即設置可能失敗的文本框值。這個錯誤正是我的想法。 webBrowser有一個加載文檔的事件。 – urlreader

回答

0

用於單擊按鈕的代碼。它需要「登錄」值並點擊它

foreach (HtmlElement el in elCol) 
       { 
        if (el.GetAttribute("value").Equals("Sign in")) 
        { 
         el.InvokeMember("click"); 
        } 
       }