Auto Website Login: SetAttribute not working
I'm creating a program dashboard and one feature is that the user is automatically logged into a website site using stored credentials in the program (no need to open chrome or FF).
In the program, await task delay is working, I see the username and password fields populate before submission (click), but when it tries to submit, the browser, built into the form, acts like the page is empty and no credentials have been entered? I should mention that I can see the username and password entered in the form, but the page acts like nothing has been entered. What am I doing wrong here?
Side note: The button on the site we're connecting to doesn't have an element ID, only a type is shown...hence the workaround for Invokemember("Click")
Any help is appreciated.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Function login_thesite() As Task
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("type") = "submit" Then
webpageelement.InvokeMember("click")
End If
Next
End Function
Private Property pageready As Boolean = False
#Region "Page Loading Functions"
Private Sub WaitForPageLoad()
AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
While Not pageready
Application.DoEvents()
End While
pageready = False
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
pageready = True
RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
End If
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
login_thesite()
WaitForPageLoad()
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Text = ""
TextBox2.Text = ""
Label3.Visible = True
WebBrowser1.Navigate("https://thesite.com/#/login")
WaitForPageLoad()
End Sub
End Class
vb.net winforms
|
show 3 more comments
I'm creating a program dashboard and one feature is that the user is automatically logged into a website site using stored credentials in the program (no need to open chrome or FF).
In the program, await task delay is working, I see the username and password fields populate before submission (click), but when it tries to submit, the browser, built into the form, acts like the page is empty and no credentials have been entered? I should mention that I can see the username and password entered in the form, but the page acts like nothing has been entered. What am I doing wrong here?
Side note: The button on the site we're connecting to doesn't have an element ID, only a type is shown...hence the workaround for Invokemember("Click")
Any help is appreciated.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Function login_thesite() As Task
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("type") = "submit" Then
webpageelement.InvokeMember("click")
End If
Next
End Function
Private Property pageready As Boolean = False
#Region "Page Loading Functions"
Private Sub WaitForPageLoad()
AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
While Not pageready
Application.DoEvents()
End While
pageready = False
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
pageready = True
RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
End If
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
login_thesite()
WaitForPageLoad()
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Text = ""
TextBox2.Text = ""
Label3.Visible = True
WebBrowser1.Navigate("https://thesite.com/#/login")
WaitForPageLoad()
End Sub
End Class
vb.net winforms
Are you awaiting hoping that the document is completed loading? Use the DocumentCompleted event.
– LarsTech
Jan 3 at 19:54
Then why do you have all that async / await stuff happening?
– LarsTech
Jan 3 at 20:15
@LarsTech Yes, the document is fully loading.
– user9289698
Jan 3 at 20:17
@larsTech I am new to vb and was troubleshooting.
– user9289698
Jan 3 at 20:17
1
Get rid of that DoEvents and the while loop. Get rid of the AddHandler and just have the methodHandles DocumentCompleted
at the end of it. The event will fire after the document is loaded. Loginthesite isn't a Task function anymore, looks like it should just be a Sub. Your button click is inspecting the document, then waiting to load the page. That doesn't sound right. You need to fix all of that before we even get to the debugging part.
– LarsTech
Jan 3 at 21:01
|
show 3 more comments
I'm creating a program dashboard and one feature is that the user is automatically logged into a website site using stored credentials in the program (no need to open chrome or FF).
In the program, await task delay is working, I see the username and password fields populate before submission (click), but when it tries to submit, the browser, built into the form, acts like the page is empty and no credentials have been entered? I should mention that I can see the username and password entered in the form, but the page acts like nothing has been entered. What am I doing wrong here?
Side note: The button on the site we're connecting to doesn't have an element ID, only a type is shown...hence the workaround for Invokemember("Click")
Any help is appreciated.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Function login_thesite() As Task
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("type") = "submit" Then
webpageelement.InvokeMember("click")
End If
Next
End Function
Private Property pageready As Boolean = False
#Region "Page Loading Functions"
Private Sub WaitForPageLoad()
AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
While Not pageready
Application.DoEvents()
End While
pageready = False
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
pageready = True
RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
End If
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
login_thesite()
WaitForPageLoad()
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Text = ""
TextBox2.Text = ""
Label3.Visible = True
WebBrowser1.Navigate("https://thesite.com/#/login")
WaitForPageLoad()
End Sub
End Class
vb.net winforms
I'm creating a program dashboard and one feature is that the user is automatically logged into a website site using stored credentials in the program (no need to open chrome or FF).
In the program, await task delay is working, I see the username and password fields populate before submission (click), but when it tries to submit, the browser, built into the form, acts like the page is empty and no credentials have been entered? I should mention that I can see the username and password entered in the form, but the page acts like nothing has been entered. What am I doing wrong here?
Side note: The button on the site we're connecting to doesn't have an element ID, only a type is shown...hence the workaround for Invokemember("Click")
Any help is appreciated.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Function login_thesite() As Task
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("type") = "submit" Then
webpageelement.InvokeMember("click")
End If
Next
End Function
Private Property pageready As Boolean = False
#Region "Page Loading Functions"
Private Sub WaitForPageLoad()
AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
While Not pageready
Application.DoEvents()
End While
pageready = False
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
pageready = True
RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
End If
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
login_thesite()
WaitForPageLoad()
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Text = ""
TextBox2.Text = ""
Label3.Visible = True
WebBrowser1.Navigate("https://thesite.com/#/login")
WaitForPageLoad()
End Sub
End Class
vb.net winforms
vb.net winforms
edited Jan 3 at 20:53
user9289698
asked Jan 3 at 19:49
user9289698user9289698
83
83
Are you awaiting hoping that the document is completed loading? Use the DocumentCompleted event.
– LarsTech
Jan 3 at 19:54
Then why do you have all that async / await stuff happening?
– LarsTech
Jan 3 at 20:15
@LarsTech Yes, the document is fully loading.
– user9289698
Jan 3 at 20:17
@larsTech I am new to vb and was troubleshooting.
– user9289698
Jan 3 at 20:17
1
Get rid of that DoEvents and the while loop. Get rid of the AddHandler and just have the methodHandles DocumentCompleted
at the end of it. The event will fire after the document is loaded. Loginthesite isn't a Task function anymore, looks like it should just be a Sub. Your button click is inspecting the document, then waiting to load the page. That doesn't sound right. You need to fix all of that before we even get to the debugging part.
– LarsTech
Jan 3 at 21:01
|
show 3 more comments
Are you awaiting hoping that the document is completed loading? Use the DocumentCompleted event.
– LarsTech
Jan 3 at 19:54
Then why do you have all that async / await stuff happening?
– LarsTech
Jan 3 at 20:15
@LarsTech Yes, the document is fully loading.
– user9289698
Jan 3 at 20:17
@larsTech I am new to vb and was troubleshooting.
– user9289698
Jan 3 at 20:17
1
Get rid of that DoEvents and the while loop. Get rid of the AddHandler and just have the methodHandles DocumentCompleted
at the end of it. The event will fire after the document is loaded. Loginthesite isn't a Task function anymore, looks like it should just be a Sub. Your button click is inspecting the document, then waiting to load the page. That doesn't sound right. You need to fix all of that before we even get to the debugging part.
– LarsTech
Jan 3 at 21:01
Are you awaiting hoping that the document is completed loading? Use the DocumentCompleted event.
– LarsTech
Jan 3 at 19:54
Are you awaiting hoping that the document is completed loading? Use the DocumentCompleted event.
– LarsTech
Jan 3 at 19:54
Then why do you have all that async / await stuff happening?
– LarsTech
Jan 3 at 20:15
Then why do you have all that async / await stuff happening?
– LarsTech
Jan 3 at 20:15
@LarsTech Yes, the document is fully loading.
– user9289698
Jan 3 at 20:17
@LarsTech Yes, the document is fully loading.
– user9289698
Jan 3 at 20:17
@larsTech I am new to vb and was troubleshooting.
– user9289698
Jan 3 at 20:17
@larsTech I am new to vb and was troubleshooting.
– user9289698
Jan 3 at 20:17
1
1
Get rid of that DoEvents and the while loop. Get rid of the AddHandler and just have the method
Handles DocumentCompleted
at the end of it. The event will fire after the document is loaded. Loginthesite isn't a Task function anymore, looks like it should just be a Sub. Your button click is inspecting the document, then waiting to load the page. That doesn't sound right. You need to fix all of that before we even get to the debugging part.– LarsTech
Jan 3 at 21:01
Get rid of that DoEvents and the while loop. Get rid of the AddHandler and just have the method
Handles DocumentCompleted
at the end of it. The event will fire after the document is loaded. Loginthesite isn't a Task function anymore, looks like it should just be a Sub. Your button click is inspecting the document, then waiting to load the page. That doesn't sound right. You need to fix all of that before we even get to the debugging part.– LarsTech
Jan 3 at 21:01
|
show 3 more comments
1 Answer
1
active
oldest
votes
You don't need any async
procedure here. The WebBrowser.DocumentCompleted event is already invoked asynchronously. DoEvents()
is equally useless if not disruptive.
You just need to subscribe to the DocumentCompleted
event and call the Navigate
method to let the WebBrowser load the remote Html resource.
When the HtmlDocument
is finally loaded, the WebBrowser will signal its completion setting its state to WebBrowserReadyState.Complete
.
About the Html Input Element and Forms:
here, the code is supposing that there's only one Form in that HtmlDocument.
It may be so, but it may be not. A Html Document can have more than one Form and each Frame can have its own Document. IFrames will have one each for sure.
Read the notes in this answer (C# code, but you just need the notes) for more informations on how to handle multiple Frames/IFrames
Button1
will wire up the DocumentCompleted
event and call Navigate()
.
When the Document is completed, the code in the event handler will run and perform the LogIn procedure.
The event handler is then removed, since it has complelted its task and you still need to use the WebBrowser for other purposes.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
WebSiteLogIn()
End Sub
Private Sub WebSiteLogIn()
AddHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
WebBrowser1.Navigate("https://thesite.com/#/login")
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allInputElements = WebBrowser1.Document.Body.All.
Cast(Of HtmlElement).Where(Function(h) h.TagName.Equals("INPUT")).ToList()
For Each element As HtmlElement In allInputElements
If element.GetAttribute("type").ToUpper().Equals("SUBMIT") Then
element.InvokeMember("click")
End If
Next
RemoveHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
Button1.Enabled = True
End If
End Sub
Your code can add multiple AddHandlers, but not always the corresponding RemoveHandler. Not sure why you would even want to juggle the AddHandler and RemoveHandler stuff. Just add the event once through the designer and be done.
– LarsTech
Jan 3 at 22:09
@LarsTech From what the OP is saying, the WebBroser is also used after the login is completed; the DocumentCompleted event will be raised for all the Web pages visited after the login. So, adding the handler and removing it after the login is completed is not wrong. What could be done here, is to disable the Button that triggers the LogIn proc and enable it again when the procedure is completed.
– Jimi
Jan 3 at 22:19
Thanks for your suggestions and input, here. I appreciate it! I'll be sure to research what you've suggested and give it a try.
– user9289698
Jan 4 at 13:40
Sure. If you have questions (directly related to this code), post a comment here.
– Jimi
Jan 4 at 13:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028879%2fauto-website-login-setattribute-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need any async
procedure here. The WebBrowser.DocumentCompleted event is already invoked asynchronously. DoEvents()
is equally useless if not disruptive.
You just need to subscribe to the DocumentCompleted
event and call the Navigate
method to let the WebBrowser load the remote Html resource.
When the HtmlDocument
is finally loaded, the WebBrowser will signal its completion setting its state to WebBrowserReadyState.Complete
.
About the Html Input Element and Forms:
here, the code is supposing that there's only one Form in that HtmlDocument.
It may be so, but it may be not. A Html Document can have more than one Form and each Frame can have its own Document. IFrames will have one each for sure.
Read the notes in this answer (C# code, but you just need the notes) for more informations on how to handle multiple Frames/IFrames
Button1
will wire up the DocumentCompleted
event and call Navigate()
.
When the Document is completed, the code in the event handler will run and perform the LogIn procedure.
The event handler is then removed, since it has complelted its task and you still need to use the WebBrowser for other purposes.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
WebSiteLogIn()
End Sub
Private Sub WebSiteLogIn()
AddHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
WebBrowser1.Navigate("https://thesite.com/#/login")
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allInputElements = WebBrowser1.Document.Body.All.
Cast(Of HtmlElement).Where(Function(h) h.TagName.Equals("INPUT")).ToList()
For Each element As HtmlElement In allInputElements
If element.GetAttribute("type").ToUpper().Equals("SUBMIT") Then
element.InvokeMember("click")
End If
Next
RemoveHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
Button1.Enabled = True
End If
End Sub
Your code can add multiple AddHandlers, but not always the corresponding RemoveHandler. Not sure why you would even want to juggle the AddHandler and RemoveHandler stuff. Just add the event once through the designer and be done.
– LarsTech
Jan 3 at 22:09
@LarsTech From what the OP is saying, the WebBroser is also used after the login is completed; the DocumentCompleted event will be raised for all the Web pages visited after the login. So, adding the handler and removing it after the login is completed is not wrong. What could be done here, is to disable the Button that triggers the LogIn proc and enable it again when the procedure is completed.
– Jimi
Jan 3 at 22:19
Thanks for your suggestions and input, here. I appreciate it! I'll be sure to research what you've suggested and give it a try.
– user9289698
Jan 4 at 13:40
Sure. If you have questions (directly related to this code), post a comment here.
– Jimi
Jan 4 at 13:43
add a comment |
You don't need any async
procedure here. The WebBrowser.DocumentCompleted event is already invoked asynchronously. DoEvents()
is equally useless if not disruptive.
You just need to subscribe to the DocumentCompleted
event and call the Navigate
method to let the WebBrowser load the remote Html resource.
When the HtmlDocument
is finally loaded, the WebBrowser will signal its completion setting its state to WebBrowserReadyState.Complete
.
About the Html Input Element and Forms:
here, the code is supposing that there's only one Form in that HtmlDocument.
It may be so, but it may be not. A Html Document can have more than one Form and each Frame can have its own Document. IFrames will have one each for sure.
Read the notes in this answer (C# code, but you just need the notes) for more informations on how to handle multiple Frames/IFrames
Button1
will wire up the DocumentCompleted
event and call Navigate()
.
When the Document is completed, the code in the event handler will run and perform the LogIn procedure.
The event handler is then removed, since it has complelted its task and you still need to use the WebBrowser for other purposes.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
WebSiteLogIn()
End Sub
Private Sub WebSiteLogIn()
AddHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
WebBrowser1.Navigate("https://thesite.com/#/login")
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allInputElements = WebBrowser1.Document.Body.All.
Cast(Of HtmlElement).Where(Function(h) h.TagName.Equals("INPUT")).ToList()
For Each element As HtmlElement In allInputElements
If element.GetAttribute("type").ToUpper().Equals("SUBMIT") Then
element.InvokeMember("click")
End If
Next
RemoveHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
Button1.Enabled = True
End If
End Sub
Your code can add multiple AddHandlers, but not always the corresponding RemoveHandler. Not sure why you would even want to juggle the AddHandler and RemoveHandler stuff. Just add the event once through the designer and be done.
– LarsTech
Jan 3 at 22:09
@LarsTech From what the OP is saying, the WebBroser is also used after the login is completed; the DocumentCompleted event will be raised for all the Web pages visited after the login. So, adding the handler and removing it after the login is completed is not wrong. What could be done here, is to disable the Button that triggers the LogIn proc and enable it again when the procedure is completed.
– Jimi
Jan 3 at 22:19
Thanks for your suggestions and input, here. I appreciate it! I'll be sure to research what you've suggested and give it a try.
– user9289698
Jan 4 at 13:40
Sure. If you have questions (directly related to this code), post a comment here.
– Jimi
Jan 4 at 13:43
add a comment |
You don't need any async
procedure here. The WebBrowser.DocumentCompleted event is already invoked asynchronously. DoEvents()
is equally useless if not disruptive.
You just need to subscribe to the DocumentCompleted
event and call the Navigate
method to let the WebBrowser load the remote Html resource.
When the HtmlDocument
is finally loaded, the WebBrowser will signal its completion setting its state to WebBrowserReadyState.Complete
.
About the Html Input Element and Forms:
here, the code is supposing that there's only one Form in that HtmlDocument.
It may be so, but it may be not. A Html Document can have more than one Form and each Frame can have its own Document. IFrames will have one each for sure.
Read the notes in this answer (C# code, but you just need the notes) for more informations on how to handle multiple Frames/IFrames
Button1
will wire up the DocumentCompleted
event and call Navigate()
.
When the Document is completed, the code in the event handler will run and perform the LogIn procedure.
The event handler is then removed, since it has complelted its task and you still need to use the WebBrowser for other purposes.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
WebSiteLogIn()
End Sub
Private Sub WebSiteLogIn()
AddHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
WebBrowser1.Navigate("https://thesite.com/#/login")
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allInputElements = WebBrowser1.Document.Body.All.
Cast(Of HtmlElement).Where(Function(h) h.TagName.Equals("INPUT")).ToList()
For Each element As HtmlElement In allInputElements
If element.GetAttribute("type").ToUpper().Equals("SUBMIT") Then
element.InvokeMember("click")
End If
Next
RemoveHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
Button1.Enabled = True
End If
End Sub
You don't need any async
procedure here. The WebBrowser.DocumentCompleted event is already invoked asynchronously. DoEvents()
is equally useless if not disruptive.
You just need to subscribe to the DocumentCompleted
event and call the Navigate
method to let the WebBrowser load the remote Html resource.
When the HtmlDocument
is finally loaded, the WebBrowser will signal its completion setting its state to WebBrowserReadyState.Complete
.
About the Html Input Element and Forms:
here, the code is supposing that there's only one Form in that HtmlDocument.
It may be so, but it may be not. A Html Document can have more than one Form and each Frame can have its own Document. IFrames will have one each for sure.
Read the notes in this answer (C# code, but you just need the notes) for more informations on how to handle multiple Frames/IFrames
Button1
will wire up the DocumentCompleted
event and call Navigate()
.
When the Document is completed, the code in the event handler will run and perform the LogIn procedure.
The event handler is then removed, since it has complelted its task and you still need to use the WebBrowser for other purposes.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
WebSiteLogIn()
End Sub
Private Sub WebSiteLogIn()
AddHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
WebBrowser1.Navigate("https://thesite.com/#/login")
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allInputElements = WebBrowser1.Document.Body.All.
Cast(Of HtmlElement).Where(Function(h) h.TagName.Equals("INPUT")).ToList()
For Each element As HtmlElement In allInputElements
If element.GetAttribute("type").ToUpper().Equals("SUBMIT") Then
element.InvokeMember("click")
End If
Next
RemoveHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
Button1.Enabled = True
End If
End Sub
edited Jan 3 at 22:54
answered Jan 3 at 21:57
JimiJimi
9,57742035
9,57742035
Your code can add multiple AddHandlers, but not always the corresponding RemoveHandler. Not sure why you would even want to juggle the AddHandler and RemoveHandler stuff. Just add the event once through the designer and be done.
– LarsTech
Jan 3 at 22:09
@LarsTech From what the OP is saying, the WebBroser is also used after the login is completed; the DocumentCompleted event will be raised for all the Web pages visited after the login. So, adding the handler and removing it after the login is completed is not wrong. What could be done here, is to disable the Button that triggers the LogIn proc and enable it again when the procedure is completed.
– Jimi
Jan 3 at 22:19
Thanks for your suggestions and input, here. I appreciate it! I'll be sure to research what you've suggested and give it a try.
– user9289698
Jan 4 at 13:40
Sure. If you have questions (directly related to this code), post a comment here.
– Jimi
Jan 4 at 13:43
add a comment |
Your code can add multiple AddHandlers, but not always the corresponding RemoveHandler. Not sure why you would even want to juggle the AddHandler and RemoveHandler stuff. Just add the event once through the designer and be done.
– LarsTech
Jan 3 at 22:09
@LarsTech From what the OP is saying, the WebBroser is also used after the login is completed; the DocumentCompleted event will be raised for all the Web pages visited after the login. So, adding the handler and removing it after the login is completed is not wrong. What could be done here, is to disable the Button that triggers the LogIn proc and enable it again when the procedure is completed.
– Jimi
Jan 3 at 22:19
Thanks for your suggestions and input, here. I appreciate it! I'll be sure to research what you've suggested and give it a try.
– user9289698
Jan 4 at 13:40
Sure. If you have questions (directly related to this code), post a comment here.
– Jimi
Jan 4 at 13:43
Your code can add multiple AddHandlers, but not always the corresponding RemoveHandler. Not sure why you would even want to juggle the AddHandler and RemoveHandler stuff. Just add the event once through the designer and be done.
– LarsTech
Jan 3 at 22:09
Your code can add multiple AddHandlers, but not always the corresponding RemoveHandler. Not sure why you would even want to juggle the AddHandler and RemoveHandler stuff. Just add the event once through the designer and be done.
– LarsTech
Jan 3 at 22:09
@LarsTech From what the OP is saying, the WebBroser is also used after the login is completed; the DocumentCompleted event will be raised for all the Web pages visited after the login. So, adding the handler and removing it after the login is completed is not wrong. What could be done here, is to disable the Button that triggers the LogIn proc and enable it again when the procedure is completed.
– Jimi
Jan 3 at 22:19
@LarsTech From what the OP is saying, the WebBroser is also used after the login is completed; the DocumentCompleted event will be raised for all the Web pages visited after the login. So, adding the handler and removing it after the login is completed is not wrong. What could be done here, is to disable the Button that triggers the LogIn proc and enable it again when the procedure is completed.
– Jimi
Jan 3 at 22:19
Thanks for your suggestions and input, here. I appreciate it! I'll be sure to research what you've suggested and give it a try.
– user9289698
Jan 4 at 13:40
Thanks for your suggestions and input, here. I appreciate it! I'll be sure to research what you've suggested and give it a try.
– user9289698
Jan 4 at 13:40
Sure. If you have questions (directly related to this code), post a comment here.
– Jimi
Jan 4 at 13:43
Sure. If you have questions (directly related to this code), post a comment here.
– Jimi
Jan 4 at 13:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028879%2fauto-website-login-setattribute-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you awaiting hoping that the document is completed loading? Use the DocumentCompleted event.
– LarsTech
Jan 3 at 19:54
Then why do you have all that async / await stuff happening?
– LarsTech
Jan 3 at 20:15
@LarsTech Yes, the document is fully loading.
– user9289698
Jan 3 at 20:17
@larsTech I am new to vb and was troubleshooting.
– user9289698
Jan 3 at 20:17
1
Get rid of that DoEvents and the while loop. Get rid of the AddHandler and just have the method
Handles DocumentCompleted
at the end of it. The event will fire after the document is loaded. Loginthesite isn't a Task function anymore, looks like it should just be a Sub. Your button click is inspecting the document, then waiting to load the page. That doesn't sound right. You need to fix all of that before we even get to the debugging part.– LarsTech
Jan 3 at 21:01