I am writing a program that needs to log in to a web site. I have used the following code in my event handler.

If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            WebBrowser1.Document.All("password").SetAttribute("value", Password)
            WebBrowser1.Document.All("email").SetAttribute("value", email1)
            'WebBrowser1.Document.All("comments").SetAttribute("value", comment1)
            WebBrowser1.Document.Forms(1).InvokeMember("submit")
        End If

the email and password fill in fine but the submit button is not activated. The web sit used the following in their HTML.

<form method="post" action="login.php" onsubmit="encrypt(this)">
			<input type="hidden" name="action" value="submit">
			<input name="salt" type="hidden" value="1269306573">
			<strong>Email Address:</strong><br />
			<input type="text" name="email" size="30" maxlength="100"><br /><br />
			<strong>Password:</strong><br />
			<input type="password" name="password" size="32" maxlength="40"><br /><br />
			<input class="button" type="submit" name="submit" value="login">
			</form>

any ideas?

Dani AI

Generated

A couple of things in this thread point to why the login keeps “blinking” or returning to the login page. First, calling InvokeMember("submit") on the <form> can bypass the page’s onsubmit handler. If the site relies on that handler (for example, to encrypt the password or add a token), the server will reject the post. In browsers, programmatic form.submit() does not fire onsubmit; triggering a click on the submit button does. So, instead of submitting the form, locate the submit <input> and call InvokeMember("click"). This preserves any JavaScript in onsubmit and typically matches a manual click. See the note that form.submit() bypasses onsubmit. MDN: HTMLFormElement submit event.

Second, the looping you describe after switching to Forms(0) is often due to DocumentCompleted firing multiple times (once per frame and again after redirects). Gate your automation on the final navigation by comparing e.Url with WebBrowser1.Url (or check the path you expect), and only then fill fields or click. Microsoft documents that DocumentComplete/DocumentCompleted fires for each frame and after redirects, which is why unguarded code can run repeatedly. . Microsoft Learn: DocumentComplete event notes.

Practical pattern you can drop in:

Private Enum StepState : Login : NextPage : Done : End Enum
Private current As StepState = StepState.Login

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If Not e.Url.Equals(WebBrowser1.Url) Then Return 'ignore frame loads

    Select Case current
        Case StepState.Login
            Dim doc = WebBrowser1.Document
            doc.GetElementById("email")?.SetAttribute("value", email1)
            doc.GetElementById("pass")?.SetAttribute("value", Password)
            Dim submitBtn = doc.GetElementsByTagName("input").
                Cast(Of HtmlElement)().
                FirstOrDefault(Function(el) el.GetAttribute("type").ToLower() = "submit")
            submitBtn?.InvokeMember("click") 'fires onsubmit
            current = StepState.NextPage

        Case StepState.NextPage
            'navigate by href found on the post-login page
            Dim link = WebBrowser1.Document.GetElementsByTagName("a").
                Cast(Of HtmlElement)().
                FirstOrDefault(Function(a) a.GetAttribute("href").Contains("/the/third/page"))
            If link IsNot Nothing Then WebBrowser1.Navigate(link.GetAttribute("href"))
            current = StepState.Done
    End Select
End Sub

Also echoing : keep AllowNavigation = True, and only run per expected page to avoid re-entrant submissions.

Recommended Answers

All 16 Replies

Did you checked encrypt(this) function s present in ur code.

How do you do that?

Is the form you're trying to submit actually the form you stated on this line: WebBrowser1.Document.Forms(1).InvokeMember("submit") ?
If the page only has one form, then the line should probably read: WebBrowser1.Document.Forms(0).InvokeMember("submit")

When I use form(0) the next page blinks on and off, it never settles out wit the page displayed.

Examine the webbrowsers LocationChanged event and perhaps also the webbrowsers DocumentCompleted event and the ProgressChanged event.

Maybe you can find some clues there as to why the form is not being submitted. Regardless if you use Forms(0) or Forms(1).

I got the log in working and navigating to a second page automatically from within the sub webbrowser1_DocumentCompleted. I still need to navigate to a third page and submit data. There is a text on the second page with the url of the third page linked to the text. I don’t know how to invoke the url for the third navigation page from within the sub webbrowser1_DocumentCompleted.

Identify the link on the second page and use the returnvalue to perform a webbrowser1.Navigate() from inside the webbrowser1.DocumentCompleted event.

Dim link As String = webbrowser1.Document.GetElementById("id of link").GetAttribute("href")
        webbrowser1.Navigate(New Uri(link))

I have tried to use the code you provided

Dim link As String = webbrowser1.Document.GetElementById("id of link").GetAttribute("href")
webbrowser1.Navigate(New Uri(link))

My original handler looks like this

Private Sub webbrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)

        AddHandler CType(sender, WebBrowser).Document.Window.Error, AddressOf Window_Error
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            WebBrowser1.Document.All("pass").SetAttribute("value", Password)
            WebBrowser1.Document.All("email").SetAttribute("value", email1)
            WebBrowser1.Document.Forms(0).InvokeMember("submit")
        End If
    End Sub

It will navigate to the log in page, submit password and email and navigate to the next page and stop.
When I change it like this.

Private Sub webbrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        Dim link As String = WebBrowser1.Document.GetElementById("id of link").GetAttribute("href")
        AddHandler CType(sender, WebBrowser).Document.Window.Error, AddressOf Window_Error
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            WebBrowser1.Document.All("pass").SetAttribute("value", Password)
            WebBrowser1.Document.All("email").SetAttribute("value", email1)
            WebBrowser1.Document.Forms(0).InvokeMember("submit")
            WebBrowser1.Navigate(New Uri(link))
        End If
    End Sub

It just navigates to the login page and enters email and stops.
I am obviously implementing the new navigation incorrectly.

Try it like this.

Private Sub webbrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser.DocumentTitle.Equals("title of loginpage") Then
        AddHandler CType(sender, WebBrowser).Document.Window.Error, AddressOf Window_Error
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            WebBrowser1.Document.All("pass").SetAttribute("value", Password)
            WebBrowser1.Document.All("email").SetAttribute("value", email1)
            WebBrowser1.Document.Forms(0).InvokeMember("submit")
        End If
    ElseIf WebBrowser.DocumentTitle.Equals("title of second page")
        Dim link As String = WebBrowser1.Document.GetElementById("id of link").GetAttribute("href")
        If Not link.Equals("") Then
            WebBrowser1.Navigate(New Uri(link))
        End If
    End If
End Sub

And remember to replace "id of link" to the actual id of the HTML element on the second page.

I have a new site and am having problems getting past the login. When I invoke "submit" it just returns to the login page with no indication of any error.

The form code is:

<form name="clearform" action="https://www.articlecat.com/secure/login.php" method="post">
	<table>
		<tr>
			<td width="33%" align="right" nowrap>Email:</td>
		  <td align="left"><input type="text" name="email" id="email" size="31" style="font-family: verdana; font-size: 9px; color: #333333; border: 1px solid #C0C0C0"></td>
		</tr>
		<tr>
			<td width="33%" align="right" nowrap>Password:</td>
		  <td align="left"><input type="password" name="pass" id="pass" size="31" style="font-family: verdana; font-size: 9px; color: #333333; border: 1px solid #C0C0C0"></td>
		</tr>
		<tr>
		  <td align="right" nowrap>&nbsp;</td>
		  <td align="left"><input type="checkbox" name="remember" id="remember" value="remember">remember me </td>
	  </tr>
		<tr>
			<td width="33%"></td>
			<td align="left" valign="middle">
				<input type="submit" value="Login" name="B7" style="color: #333333; font-family: verdana; font-size: 9px; border: 1px solid #C0C0C0">
		  </td>
		</tr>
		<tr>
		  <td></td>
		  <td>
			<a href="" rel="nofollow">Forgot Password?</a> | <a href="" rel="nofollow">Register</a>
		  </td>
	  </tr>
  </table>
</form>

my code is:

If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
                WebBrowser1.Document.All("pass").SetAttribute("value", Password)
                WebBrowser1.Document.All("email").SetAttribute("value", email1)
                WebBrowser1.Document.Forms(0).InvokeMember("submit")
                Me.Label5.Text = "Pass1"
            End If

I tried the page and whipped up a sample code to test this.

The login page uses itself as a postback form, so the page will reload every time you perform a submit from your code.
Unless you check the document source for any warning text that the login failed, the page will continue to reload.
I don't know what happens on a successful login, so I have no reference to that end, whether or not you will be redirected to a new page.

In conclusion.
You have to add code before you set the values and submit, to check the document source for any text or information so that you can decide whether or not to submit the form.

I have looked at the code for the site and can't figure out what to do. Here is the code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<META NAME="DESCRIPTION" CONTENT="Free web content articles in our article directory and catalog for your website. Read, submit, and publish our articles for free.">
<title>Article Directory and Catalog | Login</title>
<!-- embed_css reminder --> 
<link rel="Stylesheet" href="https://www.articlecat.com/mainstyle.css" type="text/css" media="screen">
</head>
<body>
<script type="text/javascript">
window.google_analytics_uacct = "UA-247184-17";
window.google_analytics_domain_name = ".articlecat.com";
</script>
<div id="bg">
<table align="center" class="maintable" cellspacing="0" cellpadding="0">
	<tr>
		<td align="center" class="topnav" width="100%">
			<a href="" rel="nofollow"><img border="0" src="/images/logo.gif" width="427" height="100" alt="Free Articles Directory"></a>
		</td>
		<td width="200px">
			<br><br><br>
		</td>
	</tr>
	<tr>
		<td width="100%" class="maincontentcell"><div class="page_title">Login</div>

<p>To login please enter your email and password in the form below.</p>
<br><br><br>
<center>
<form name="clearform" action="https://www.articlecat.com/secure/login.php" method="post">
	<table>
		<tr>
			<td width="33%" align="right" nowrap>Email:</td>
		  <td align="left"><input type="text" name="email" id="email" size="31" style="font-family: verdana; font-size: 9px; color: #333333; border: 1px solid #C0C0C0"></td>
		</tr>
		<tr>
			<td width="33%" align="right" nowrap>Password:</td>
		  <td align="left"><input type="password" name="pass" id="pass" size="31" style="font-family: verdana; font-size: 9px; color: #333333; border: 1px solid #C0C0C0"></td>
		</tr>
		<tr>
		  <td align="right" nowrap>&nbsp;</td>
		  <td align="left"><input type="checkbox" name="remember" id="remember" value="remember">remember me </td>
	  </tr>
		<tr>
			<td width="33%"></td>
			<td align="left" valign="middle">
				<input type="submit" value="Login" name="B7" style="color: #333333; font-family: verdana; font-size: 9px; border: 1px solid #C0C0C0">
		  </td>
		</tr>
		<tr>
		  <td></td>
		  <td>
			<a href="" rel="nofollow">Forgot Password?</a> | <a href="" rel="nofollow">Register</a>
		  </td>
	  </tr>
  </table>
</form>
</center>
<script type="text/javascript">
	document.clearform.email.focus();
</script></td>
<td class="navcell">
<div id="navcontainer">
<br><br>
	<div class="sectionheader">Authors</div>
	<ul id="navigation2">
		<li><a href="https://www.articlecat.com/secure/login.php" rel="nofollow">Login</a>&nbsp;|&nbsp;<a href="" rel="nofollow">Signup</a></li>
		<li><a href="" rel="nofollow">Submit Articles</a></li>
		<li><a href="" rel="nofollow">Terms and Conditions</a></li>
	</ul>
	<br>
	<div class="sectionheader">Publishers &amp; Readers</div>
	<ul id="navigation3">
		<li><a href="">Subscribe to RSS feed</a></li>
		<li><a href="" rel="nofollow">Top 100 Authors</a></li>
		<li><a href="" rel="nofollow">Publishing Guidelines</a></li>
	</ul>
	<br>
	<div class="sectionheader">Site</div>
	<ul id="navigation4">
		<li><a href="" rel="nofollow">About Us</a></li>
		<li><a href="" rel="nofollow">Contact Us</a></li>
		<li><a href="" rel="nofollow">Link to Us</a></li>
		<li><a href="" rel="nofollow">Terms of Service</a></li>
		<li><a href="" rel="nofollow">Privacy Policy</a></li>
	</ul>
<br>
<br>

</div></td>
</tr>
</table>
</div>
<center>
<font face="Verdana" size="1">&copy; 2006-2010 articlecat.com, All rights reserved.
<br>
<a href="">Free articles directory</a> | <a href="">Free articles</a> | <a href="">Submit articles</a> | <a href="" target="_blank" rel="nofollow">Terms and Conditions</a>
</font>
</center>
<script type="text/javascript"> 
 
  var _gaq = _gaq || []; 
  _gaq.push(['_setAccount', 'UA-247184-17']); 
  _gaq.push(['_setDomainName', '.articlecat.com']);   
  _gaq.push(['_trackPageview']); 
  _gaq.push(['_trackEvent', 'calc', 'great', 'www.articlecat.com/secure/login.php', 41]);
 
  (function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? '' : '') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
  })(); 
 
</script>
</body>
</html>

If you attempt a login but it failes, the source will be different.
This is what you need to check for.

<td width="100%" class="maincontentcell"><div class="page_title">Login</div>
 
		<br><br>
		<hr>
		<b>Warning</b> - Your login attempt failed. The username and/or password you entered are not valid.
		<br>
		<br>
		Please make sure that you are using the correct email address as your user name.<br>
		<br>
		You can try to login again by entering your username and password in the login box below.
		<br><br>
		If you forgot your password you can recover it here <a href="">recover password</a>
		<hr>
		<br><br>
		<br><br>
		<br><br>
		
<p>To login please enter your email and password in the form below.</p>
<br><br><br>
<center>
<form name="clearform" action="https://www.articlecat.com/secure/login.php" method="post">

if I use the following code:

If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
                WebBrowser1.Document.All("pass").SetAttribute("value", Password)
                WebBrowser1.Document.All("email").SetAttribute("value", email1)
                'WebBrowser1.Document.Forms(0).InvokeMember("submit")
                Me.Label5.Text = "Pass2"

the login page appears with my email and password filled in and if I manualy hit the submit button I go to the correct page to continue filling in info.
If I uncomment the invoke line I get the login page without any info filled in. It appears that the invoke command is causing the page to reappear.

Also If I submit an incorrect password and manualy hit the submit button I get the page that you indicated.
But if I use the invoke line I just get the blank login page as before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.