2011-08-12 51 views
1

我试图使用WebBrwoser控件以编程方式登录到https://www.salesgenie.com/Account/LogOn使用WebBrowser控件以编程方式登录到网站的问题

问题是当我点击“登录”时,浏览器没有导航到LogonCompleted事件中的下一页。

HtmlElement userName = wBrowser.Document.GetElementById("username"); 
userName.SetAttribute("value", SomeUserName); 

HtmlElement password = wBrowser.Document.GetElementById("password"); 
password.SetAttribute("value", SomePassword); 

wBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(LogonPageLoaded); 
wBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LogonCompleted); 

HtmlElement logonForm = wBrowser.Document.GetElementById("logon-submit"); 
logonForm.InvokeMember("click"); 

我认为这是因为元素“登录提交”调用JavaScript函数左右。

请任何帮助,将不胜感激。

回答

2

检查出你的四号线,你又设置userName时,您应该设置password

编辑

除了上面的问题我猜你想调用点击太快了。该按钮是使用JavaScript创建的,因此在点击之前必须稍等一会。不幸的是,尽管您可能会测试各种属性,但您可以通过监听来确定JavaScript是否为done。最安全的是在调用click之前可能只是在加载后几秒钟。

下面的代码适用于我(虽然我没有有效的用户名和密码)。我有一个名为button1的按钮和一个名为webBrowser1的浏览器。一旦页面在浏览器中可视化加载,单击表单上的按钮可正确调用浏览器中的click事件。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      this.webBrowser1.Navigate("https://www.salesgenie.com/Account/LogOn"); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      string SomeUserName = "Test"; 
      string SomePassword = "Test"; 

      HtmlElement userName = webBrowser1.Document.GetElementById("username"); 
      Console.WriteLine(userName.GetAttribute("value")); 
      userName.SetAttribute("value", SomeUserName); 
      userName.RemoveFocus(); 

      HtmlElement password = webBrowser1.Document.GetElementById("password"); 
      Console.WriteLine(userName.GetAttribute("value")); 
      password.SetAttribute("value", SomePassword); 

      HtmlElement logonForm = webBrowser1.Document.GetElementById("logon-submit"); 
      logonForm.InvokeMember("click"); 
     } 
    } 
} 
+0

谢谢克里斯......但这不是问题。 – YAM

+0

有什么建议吗? – YAM

+0

实际上,我完全没有点击“登录 - 提交”按钮。 – YAM

0

你试过用logonForm.click(); ?没有调用任何成员?