finding web page source at desired instance VB.NET - vb.net

IN CODE: At 1 browser clicks a button and takes a time to load. AT 2 i get source code of page in RichTextBox1. but as page take time to load code 2 starts before completion of 1 because of that i am unable to get the web page source at desired state? what do i do ? i want to get web page source when web browser completely loads after the execution of code 1.
i have tried
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WebBrowser1.Navigate("some website")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Document.GetElementById("notification_address").SetAttribute("value", TextBox1.Text)
WebBrowser1.Document.Forms(0).InvokeMember("submit") (-----1-----)
RichTextBox1.Text = WebBrowser1.DocumentText (-----2-----)
End Sub

You can use the DocumentCompleted event.
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Private Sub DocumentCompleted(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' this is where your code goes RichTextBox1.Text = WebBrowser1.DocumentText (-----2-----)
End Sub

Related

How do I login Via a Button using VB and a Web Browser Element

I've added a web browser element to my form and have been able to enter information into the box. When entering the correct information the webpage tries to download, how do I stop this from happening?
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim usernameTextBox As HtmlElement = Me.WebBrowser1.Document.All.Item("Email")
usernameTextBox.InnerText = "******"
Dim passwordTextBox As HtmlElement = Me.WebBrowser1.Document.All.Item("Password")
passwordTextBox.InnerText = "******"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate(New Uri("https://squareup.com/terminal/"))
End Sub
End Class
The output tries to download the webpage Image

Is there a Form event like "Shown" but runs the code every time it is displayed instead of the just first time?

Just noticed in one of my programs that the form event Shown only runs the code when the form is first displayed. Every other time after that it does not run the code. Is there a form event that does what shown does, but every time it is displayed instead of just the first? Or is there a way to get around it? This is the exact same for the Load event too.
Thanks heaps, appreciate any response.
I just tested with this in Form1:
Private f2 As New Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
f2.Show()
End Sub
and this in Form2:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Form2_Load")
End Sub
Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Visible Then
MessageBox.Show("Form2_VisibleChanged")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Hide()
End Sub
and it worked exactly as expected. Every time I clicked Button1 in Form1, Form2 was displayed. The first time I saw messages for "Form2_Load"and "Form2_VisibleChanged" and on subsequent occasions on for "Form2_VisibleChanged".

How to automatically input in the WebBrowser control

I'm quite very amateur in vb.net. When I type a text on textbox, it should be able to automatically input on the webbrowsercontrol and also how to click the button signin, wherein no getelementbyid.
Also I manage to get the 1st part correct, but when I click sign in button from inside browser, there seems to be a minor error. I've made a project like this one before long long time ago and can't find the source code of it anymore, so I'm starting from scratch again.
Website: https://app.coins.ph/welcome/login
Heres my code so far:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
WebBrowser1.Document.GetElementById("username").InnerText = TextBox1.Text
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("class") 'Depending on how the source code is formatted on the tag, you may also try Element.OuterHTML, Element.InnerText and Element.OuterText in the line below
If Element.OuterText.Contains("SIGN IN") Then
Element.InvokeMember("click")
Exit For
End If
Next Element
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
WebBrowser1.Document.GetElementById("password").InnerText = TextBox2.Text
End Sub
You've got a couple of issues here. Let's start with the textbox inputs. If you look at the html source for that website's sign-in page, the inputs for username and password do not have an ID property, they only use Name. Moreover, GetElementsByTagName is searching for a html element of "username", not an "input" as it should. Given both of those issues, you should be using Document.All("[elementName]") to access those inputs. As for the sign-in part, as stated before, GetElementsByTagName is looking for html elements, so searching for the value "class" is not going to return anything you want. Instead, you should be looking for a "button" where the OuterText contains "SIGN IN". With all those changes applied, the code becomes:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("https://app.coins.ph/welcome/login")
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
WebBrowser1.Document.All("username").InnerText = TextBox1.Text
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
WebBrowser1.Document.All("password").InnerText = TextBox2.Text
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
If Element.OuterText.Contains("SIGN IN") Then
Element.InvokeMember("click")
Exit For
End If
Next Element
End Sub
End Class
There is another problem once you run this code though
If you run the above example, you will see that the form-fields are properly filled in and the sign-in button is clicked successfully, however an error appears indicating that the form fields are still blank. Even if you use WebBrowser1.Document.All("username").SetAttribute("value", TextBox1.Text) to set the input's value as well, the same error occurs. This is likely because the website's developers are using some sort of javascript that is detecting keypresses for one reason or another...it's impossible to know why, but that's how it is. So you're left with actually simulating key presses yourself. If you do that, the website will successfully log in with the username and password. You have two ways of doing this. The cleaner way is to just send all the keys at once and log in like so:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Focus()
WebBrowser1.Document.All("username").Focus()
For Each c As Char In TextBox1.Text.ToCharArray
SendKeys.SendWait(c)
Next
WebBrowser1.Document.All("password").Focus()
For Each c As Char In TextBox2.Text.ToCharArray
SendKeys.SendWait(c)
Next
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
If Element.OuterText.Contains("SIGN IN") Then
Element.InvokeMember("click")
Exit For
End If
Next Element
End Sub
However, if you still want each character to appear as you type, to mirror the functionality of the TextChanged event logic you are currently using you would have to use the KeyPress event and basically forward the keystrokes like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
If Element.OuterText.Contains("SIGN IN") Then
Element.InvokeMember("click")
Exit For
End If
Next Element
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
WebBrowser1.Focus()
WebBrowser1.Document.All("username").Focus()
SendKeys.SendWait(e.KeyChar)
TextBox1.Focus()
End Sub
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
WebBrowser1.Focus()
WebBrowser1.Document.All("password").Focus()
SendKeys.SendWait(e.KeyChar)
TextBox2.Focus()
End Sub

WebBrowser load and display URLs from file

I want to load a list of URLs in a text file. I have done that using a For Each loop, but I want the browser to load the page first fully before going to the next URL. Here is my code:
For Each url in File.ReadAllLines("urls.txt")
Browser.Navigate(url)
Next
It doesn't display anything. I tried using:
Do While Browser.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
It displays the page but the first line is the only one being loaded.
If you want to navigate to each URL after the other you are better of storing all of them in a class level array and subscribing to the WebBrowser.DocumentCompleted event, then keep track of which URL index you're currently at via a class level Integer variable.
When the DocumentCompleted event is raised you just increment the integer variable and load the URL from the next item of the array.
Public Class Form1
Dim URLs As String()
Dim UrlIndex As Integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
URLs = File.ReadAllLines("urls.txt")
WebBrowser1.Navigate(URLs(UrlIndex))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
UrlIndex += 1
If UrlIndex >= URLs.Length Then
UrlIndex = 0 'Go back to the beginning.
End If
WebBrowser1.Navigate(URLs(UrlIndex))
End Sub
End Class
To add a delay to it to make each URL show for a little while you can use a Timer (Credit to Werdna for bringing up the subject):
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
UrlIndex += 1
If UrlIndex >= URLs.Length Then
UrlIndex = 0 'Go back to the beginning.
End If
WebBrowser1.Navigate(URLs(UrlIndex))
Timer1.Stop()
End Sub
Just set the timer's Interval property to change how long a site should be displayed (in milliseconds).

Trying to enter a name in a textbox, hit ok, and have that form close and pass the info to another forms label

***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub