Question

I have a program I have created for work that keeps track of information. We get this information from various intranet sites. I am wanting to include a form on my program that will essentially be a customized web browser that allows me to navigate those sites more easily. This is a learning project for me so please forgive my inexperience.

For this question I am looking for a way after loading a page with the browser control I can log in to that page from credentials stored in a string. I do not have access to the HTML code of the site I am trying to input this information in to and have seen many tutorials on how to do this via GetElementId but after using Firebug I found neither of the two textboxes I need to input into have an id (that I can see anyway). The site never changes and I did consider using the GetElementFromPoint but not sure how to go about it this way and it did not seem optimal. GetElementsByTagName seems to be the best way of going about it but am unsure of how to implement the code.

Here is the information of the two elements i was able to get from firebug.

Username textbox: <input type="text" name="j_username" size="12" onblur="this.value=this.value.toLowerCase(); setUserIdCookie();" click="this.value=this.value.toLowerCase();" onkeyup="this.value=this.value.toLowerCase();">

Password textbox:

<input type="password" onkeypress="checkCapsLock( event )" name="j_password" size="12">

So i need to pass for example from my Winform usernameString to the j_username and then passwordString to the j_password and then click the <input type="submit" name="submit" value="Login">button within the web control. If i can get an idea of how to go about this from this one site I should be able to learn enough from it to implement in the other sites as well.

Thanks in advance for any assistance and please let me know if any other information is required please.

The full HTML Code:

<form method="POST"action="j_security_check" name="login"> 
<table border="0"width="30%"cellspacing="3"cellpadding="2">
  <tr>
     <td class="default"><b>Login</b></td>      
     <td class="default"><input onkeyup="this.value=this.value.toLowerCase();"
      click="this.value=this.value.toLowerCase();" 
      onblur="this.value=this.value.toLowerCase(); setUserIdCookie();"    
       type="text"size="12" name="j_username"></td>     
  </tr>     
  <tr>      
      <td class="default"><b>Password</b></td>      
      <td class="default"><input type="password" size="12"
      name="j_password"onKeyPress="checkCapsLock( event )"></td>    
  </tr>     

  <tr>      
  <td colspan="2" align="center"><p class="default">  
  <inputtype="submit"value="Login"name="submit"></td>   
  </tr>
  </table> 
  </form>

EDIT

I have found a way to input in to the boxes

            var x = webBrowser1.Document.All.GetElementsByName("j_username");
        x[0].InnerText = (usernameString);
        var y = webBrowser1.Document.All.GetElementsByName("j_password");
        y[0].InnerText = (passwordString);

But am having trouble figuring out how to click the Login button

Was it helpful?

Solution

Of course after posting this nice long post and working on this for hours i finally stumbled upon the answer. For anyone who is having the same issue I was here is a way to accomplish this. In my scenario this solution works well:

private void button4_Click(object sender, EventArgs e)
{
   //Set username and password strings
   string usernameString = "username";
   string passwordString = "password";

   //Input in to username field
   var x = webBrowser1.Document.All.GetElementsByName("j_username");
   x[0].InnerText = (usernameString);

   //Input in to password fields
   var y = webBrowser1.Document.All.GetElementsByName("j_password");
   y[0].InnerText = (passwordString);

   //Click the login button
   var s = webBrowser1.Document.All.GetElementsByName("submit");
   s[0].InvokeMember("click");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top