2017-03-05 122 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"); 
        } 
       }