我需要在Web应用程序中使用c#web浏览器控件登录网站。该网站有JavaScript重定向。当调用InvokeMember("单击")时,它不会触发。我的代码如下:
public Form2()
{
SetBrowserFeatureControl();
InitializeComponent();
}
int pages = 0;
private void btnLoginEc_Click(object sender, EventArgs e)
{
pages = 1;
webBrowser1.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
webBrowser1.Navigate("https://www.example.com/login");
//webBrowser1.Refresh(WebBrowserRefreshOption.Completely);
}
void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//WebBrowser webBrowser1 = sender as WebBrowser;
if (webBrowser1.ReadyState != WebBrowserReadyState.Complete) return;
if(pages == 1)
{
pages = 2;
//login();
HtmlElement userNameEle = webBrowser1.Document.GetElementById("pt1:username::content");
HtmlElement passwordEle = webBrowser1.Document.GetElementById("pt1:password::content");
HtmlElement logonButtonEle = webBrowser1.Document.GetElementById("pt1:logon");
HtmlElement formEle = webBrowser1.Document.GetElementById("f1");
if (userNameEle != null && passwordEle != null && logonButtonEle != null)
{
userNameEle.SetAttribute("Value", "username");
passwordEle.SetAttribute("Value", "password");
logonButtonEle.InvokeMember("click");
}
}
else if(pages == 2)
{
pages = 3;
//gotoData();
HtmlElement someEle1 = webBrowser1.Document.GetElementById("pt1:it1::content");
HtmlElement someEle2 = webBrowser1.Document.GetElementById("pt1:id1::content");
HtmlElement verifyButtonEle = webBrowser1.Document.GetElementById("pt1:verify");
if(someEle1 != null && someEle2 != null && verifyButtonEle != null)
{
someEle1.SetAttribute("Value", "somevalue");
someEle2.SetAttribute("Value", "somevalue");
verifyButtonEle.InvokeMember("click");
}
}
}