I need to login to a web site using c# web browser control in an web application. The site has javascript redirects. When InvokeMember("click") is called, it does not fire. My code is as follows:
public Form2()
{
SetBrowserFeatureControl();
InitializeComponent();
}
int pages = 0;
private void btnLoginEc_Click(object sender, EventArgs e)
{
pages = 1;
webBrowser1.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
webBrowser1.Navigate("http://ift.tt/1o6ieYS");
//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");
}
}
}
- I need to login to site first (Login button is not clicked).
- Then I put some value in the controls
- Then Verify button needs to be clicked
Aucun commentaire:
Enregistrer un commentaire