We help IT Professionals succeed at work.

Web Browser Control in C#.net

Help2011
Help2011 asked
on
Looking for help in WebBrowser Control.
 Iam trying to log in to URL(below is the url) by using user name and password(i have those credentials).
URL: https://ime-ediss.noridian.com/iowaxchange/LogonDisplay.do
 
After sussesfull login i need to navigate to other link to submit request(Below is the link) like firstname ,lastname..etc.

https://ime-ediss.noridian.com/iowaxchange/logon.do;jsessionid=83CC5BBE2CE82DB92EEFC5A7CD97E1D8

Attached are the screen.
 
Looking for for help.Thansk in advance.


mytask.docx
Comment
Watch Question

Sounds like you want to use the Navigated event of the WebBrowser control: http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigated.aspx

When the event fires you should check that the login details entered are correct (check that you are navigating to the success page for instance or if you have a session ID) and then just navigate to your new URL using the Navigate method (in the example).

Author

Commented:
Thanks for your help .
Iam getting error :
"Object reference not set to an instance of an object."
How can i get name of the username and password text box's??Seems to be the name of the textbox for username and password is wrong.

I am very new to .Net please help me step by step .

Here is my code:
  public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("https://ime-ediss.noridian.com/iowaxchange/LogonDisplay.do");
            webBrowser1.Document.GetElementById("username").SetAttribute("value", "UN0002");
            webBrowser1.Document.GetElementById("password").SetAttribute("value", "Pass@30");
            webBrowser1.Document.Links["imgsubmit"].InvokeMember("Click");

        }
        private void Click(object sender, EventArgs e)
        {
            //Relace with your web link.
            webBrowser1.Navigate(@"https://ime-ediss.noridian.com/iowaxchange/logon.do;jsessionid=83CC5BBE2CE82DB92EEFC5A7CD97E1D8");
        }

Thansk,
You were on the right path but you're getting the object null reference error not because the controls aren't there, but the document hasn't loaded yet.

1. You need to attach to the DocumentCompleted event and then access the controls.
2. The submit button does not have a name/ID so that was not being found (used the index which isn't safe but works in this example).
3. After the login forms posts back you should check if you logged in properly (left that up to you).
4. When you redirect you should probably use the current session token in the cookie, your URL token may not be valid but I'm not sure what you're trying to do here.


Your code should look something like this when you're done:
 
public Form1()
{
  InitializeComponent();
  webBrowser1.DocumentCompleted += Home_DocumentCompleted;
  webBrowser1.Navigate("https://ime-ediss.noridian.com/iowaxchange/LogonDisplay.do");
}

private void Home_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  // Not interested in the home page loading anymore.
  webBrowser1.DocumentCompleted -= Home_DocumentCompleted;

  // Now wait for the login to complete.
  webBrowser1.DocumentCompleted += Login_DocumentCompleted;

  webBrowser1.Document.GetElementById("username").SetAttribute("value", "UN0002"); 
  webBrowser1.Document.GetElementById("password").SetAttribute("value", "Pass@30");
  webBrowser1.Document.Links[0].InvokeMember("Click");
}

private void Login_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  // Disconnect from the loaded event.
  webBrowser1.DocumentCompleted -= Login_DocumentCompleted;

  // Do some validation here to make sure you've logged in (just passing it through as an example).
  if (true)
  {
    //webBrowser1.Navigate(@"https://ime-ediss.noridian.com/iowaxchange/logon.do;jsessionid=83CC5BBE2CE82DB92EEFC5A7CD97E1D8");
    // Sure you don't want to use the cookie value for the session ID here???
    webBrowser1.Navigate(@"https://ime-ediss.noridian.com/iowaxchange/logon.do;" + webBrowser1.Document.Cookie);
  }
}

Open in new window

Author

Commented:
Thanks for your time,I will try this code.
really appreciated your time sir.

Author

Commented:
DocumentCompleted is not fired.Please help me here is my code:

public override string SendTransactionAndGetRouteResponse()
        {
            Form objForm = new Form();
            WebBrowser wb= new WebBrowser();
            wb.DocumentCompleted += Home_Documnt;
            wb.Visible = true;
            wb.Navigate("https://ime-ediss.noridian.com/iowaxchange/LogonDisplay.do");
            objForm.Controls.Add(wb);
            objForm.Show();
            string response = "Some thing";
            return response ;
        }

        private void Home_Documnt(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.DocumentCompleted -= Home_Documnt;
            webBrowser1.Document.GetElementById("username").InnerText = "User";
            webBrowser1.Document.GetElementById("password").InnerText = "Pass";
            webBrowser1.Document.GetElementById("imgsubmit").InvokeMember("Click");

        }


Thanks.
Firstly webBrowser1 and the control you create called wb are not the same instance.

Secondly, you are creating a form called objForm and dynamically adding a WebBrowser control to it, once the SendTransactionAndGetRouteResponse method exits both the form, and the control you added to it, go out of scope so no events will fire on it. The objects are basically gone now along with all attached delegates.

You need to keep this objects in scope by declaring them outside the method.
 
// Variables declared outside so their lifetime is longer than just the method call.
Form objForm = null;
WebBrowser wb = null;

public override string SendTransactionAndGetRouteResponse()
{
  // By newing again you are getting rid of any previous control and event on them.
  objForm = new Form();
  wb = new WebBrowser();
  wb.DocumentCompleted += Home_Documnt;
  wb.Visible = true;
  wb.Navigate("https://ime-ediss.noridian.com/iowaxchange/LogonDisplay.do");
  objForm.Controls.Add(wb);
  objForm.Show();
  string response = "Some thing";
  return response;
}

private void Home_Documnt(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  // Use the global reference to the WebBrowser control here, not the other one added from the designer.
  wb.DocumentCompleted -= Home_Documnt;
  wb.Document.GetElementById("username").InnerText = "User";
  wb.Document.GetElementById("password").InnerText = "Pass";
  wb.Document.GetElementById("imgsubmit").InvokeMember("Click");
}

Open in new window

Author

Commented:
Thank you so much. I am using this code is windows service. The above code is working perfect.
Iam using 3 delegets for my task.

Step 1 log in form submit.
Step 2 clicking on Eligibility button
Step 3 fill the form : Enter data and submit

If you can see the 3rd(Please see the attachment) form it has a drop down “profile name” which is mandatory.
 if I dint select that field, a pop up dialog box will show “select profile name “  As soon as I click on submit button on this from…As it was mandatory.

How can I handle this kind of dialog box’s?

Thanks.
If you are using this in a service then you shouldn't be using any form controls at all since the use may not be present to click on dialogs or close the form on failure. Remove your Form reference and just use the WebBrowser control, it should work and be in the background.

As for your javascript alert that's showing up, without user interaction there is no way you can close this. I'm not sure if you can do anything if the form isn't even visible and no-one is logged in. Your best bet is to disable javascript on the WebBrowser control and make sure all your automation works without triggering any script that could be blocking the server application.

Author

Commented:
Yes you are true ,With out from object also its working fine.
But how can i disable javascript on the WebBrowser control?
Can you share me the steps?

Thanks.
Add the following method to your and call it at the top of each document complete event to disable alerts:
 
private void InjectAlertBlocker()
{
  HtmlElement head = wb.Document.GetElementsByTagName("head")[0];
  HtmlElement scriptEl = wb.Document.CreateElement("script");
  IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
  string alertBlocker = "window.alert = function () { }";
  element.text = alertBlocker;
  head.AppendChild(scriptEl);
}

Open in new window

I haven't tested this but it makes sense to replace the javascript alert function with a blank, it came from this article.

Author

Commented:
I am getting error for Error IHTMLScriptElement.

      1      The type or namespace name 'IHTMLScriptElement' could not be found (are you missing a using directive or an assembly reference?)      

Thanks,

Author

Commented:
Ok got it i add reference mshtml.

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for Help2011's comment http:/Q_27244798.html#36353507

for the following reason:

thanks

Author

Commented:
thanks
If the answers assisted you in any way with resolving the problem, please allocate points accordingly and close the question. Glad I could help.
#3

Primary answer: 36331309
Clarification with full source code example: 36334160
Fixed user submitted code: 36346390
Answer and solution to secondary question: 36353459

Help2011 implied that the answers helped with the complete resolution of the main question and sub questions that followed. This was possibly closed in error.