Capture New Window in WebBrowser Control VB.NET - vb.net

I've been searching a bit and I haven't been able to exactly find what I need. I need to contain a popup window within the WebBrowser control in VB.NET
I found this project: http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx
But I've been having trouble parsing out what I need from it. It looks like it implements what I need, but I'm not really sure how the heck it's doing it. I just need to capture a popup and display it in a new WebBrowser object.

Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
Dim myelement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myelement.GetAttribute("href")
Dim newinstance As New WebBrowser
newinstance.Show()
newinstance.Navigate(target)
e.Cancel = True
End Sub

Related

Wait for internet explorer to load everything?

I am scraping a webpage, and waiting for internet explorer to get done loading but for some reason it is not. I'm trying to get a value on the page but the wait part is not waiting therefore the value comes back blank when there should be a value. The IE page has done loading but the value for the elements on the page has not been loaded yet. Is there a way to wait for all elements to get done loading before proceeding to next line of code? Here's my code:
Dim IE As Object
Dim myvalue as string
IE = CreateObject("internetexplorer.application")
IE.navigate("mypage")
While Not IE.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
End While
myValue = IE.document.getElementById("theValue").getAttribute("value")
Debug.Print(myValue)
You SHOULD NOT use Application.DoEvents() in order to keep your UI responsive! I really can't stress this enough! More than often using it is a bad hack which only creates more problems than it solves.
For more information please refer to: Keeping your UI Responsive and the Dangers of Application.DoEvents.
The correct way is to use the InternetExplorer.DocumentComplete event, which is raised when the page (or a sub-part of it, such an an iframe) is completely loaded. Here's a brief example of how you can use it:
Right-click your project in the Solution Explorer and press Add Reference...
Go to the COM tab, find the reference called Microsoft Internet Controls and press OK.
Import the SHDocVw namespace to the file where you are going to use this, and create a class-level WithEvents variable of type InternetExplorer so that you can subscribe to the event with the Handles clause.
And voila!
Imports SHDocVw
Public Class Form1
Dim WithEvents IE As New InternetExplorer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
IE.Navigate("http://www.google.com/")
End Sub
Private Sub IE_DocumentComplete(pDisp As Object, ByRef URL As Object) Handles IE.DocumentComplete
MessageBox.Show("Successfully navigated to: " & URL.ToString())
End Sub
End Class
Alternatively you can also subscribe to the event in-line using a lambda expression:
Imports SHDocVw
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim IE As New InternetExplorer
IE.Navigate("http://www.google.com/")
AddHandler IE.DocumentComplete, Sub(pDisp As Object, ByRef URL As Object)
MessageBox.Show("Successfully navigated to: " & URL.ToString())
End Sub
End Sub
End Class

Manipulate text in a loaded html-page of the Webbrowser control in the DocumentCompleted event (vb.net)

I already tried some solutions provided here, but I cannot get it to work. On my winform I have a webbrowser control which should load a webpage (aspx). In case the webpage isn't found, I want to let the user know that this page isn't found. To get this to work I use the following code:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.Document.Url.ToString().StartsWith("res:")) Then
Dim curDir As String = Directory.GetCurrentDirectory()
Dim Url As Uri = New Uri(String.Format("file:///{0}/Html/PageNotFound.html", curDir))
WebBrowser1.Navigate(Url)
End If
End Sub
This is working fine. The page PageNotFound.html is shown. However, I would like to provide the user with some additional information which I want to insert into the PageNotFound.html at realtime (i.e. using document.getElementById to manipulate a Label-tag). I just don't know how I can do this, or if it is even possible. Maybe I use the wrong event. something I also tried is:
With WebBrowser1
.Navigate("about:blank")
.Document.OpenNew(False)
.Document.Write(HtmlString)
.Refresh()
End With
Where the HtmlString contains a complete webform.(like: "")
Maybe someone put me the right direction? TIA

VB.NET WebBrowser Control Programmatically Filling Form After Changing User-Agent (Object reference not set to an instance of an object.)

I'm working on a project where I have a WebBrowser control which needs to have a custom user-agent set, then go to Google and fill out the search box, click the search button, then click a link from the search results. Unfortunately I can't use HTTPWebRequest, it has to be done with the WebBrowser control.
Before I added the code to change the user-agent, everything worked fine. Here's the code that I have:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("urlmon.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function UrlMkSetSessionOption(dwOption As Integer, pBuffer As String, dwBufferLength As Integer, dwReserved As Integer) As Integer
End Function
Const URLMON_OPTION_USERAGENT As Integer = &H10000001
Public Sub ChangeUserAgent(Agent As String)
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0)
End Sub
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
ChangeUserAgent("Fake User-Agent")
wb.Navigate("http://www.google.com", "_self", Nothing, "User-Agent: Fake User-Agent")
End Sub
Private Sub wb_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted
Dim Source As String = wb.Document.Body.OuterHtml
Dim Uri As String = wb.Document.Url.AbsoluteUri
If Uri = "http://www.google.com/" Then
wb.Document.GetElementById("lst-ib").SetAttribute("value", "browser info")
wb.Document.All("btnK").InvokeMember("click")
End If
If Uri.Contains("http://www.google.com/search?") Then
Dim TheDocument = wb.Document.All
For Each curElement As HtmlElement In TheDocument
Dim ctrlIdentity = curElement.GetAttribute("innerText").ToString
If ctrlIdentity = "BROWSER-INFO" Then
curElement.InvokeMember("click")
End If
Next
End If
End Sub
End Class
The problem lies in the following code:
wb.Document.GetElementById("lst-ib").SetAttribute("value", "browser info")
wb.Document.All("btnK").InvokeMember("click")
I thought the problem might be that the page not being fully loaded (frame issue) but I put the offending code in a timer to test, and got the same error. Any help would be much appreciated.
Do you realize .All("btnK") returns a collection? So, you are doing .InvokeMember("click") on a Collection :). You cannot do that, you can only do .InvokeMember("click") on an element for obvious reasons!
Try this:
wb.Document.All("btnK").Item(0).InvokeMember("click")
The .Item(0) returns the first element in the collection returned by .All("btnK"), and since there will only probably be one item returned, since there is only one on the page, you want to do the InvokeMember on the first item, being .Item(0).
May I ask what it is you are developing?
Since you're a new user, please up-vote and/or accept if this answered your question.

Visual Basic - Web browser load URLs from text

i am not so great with Visual basic, but i need some help on creating a web browser that would load several links import from a text file, and for the web browser to navigate to them. This is what i have so far
Dim link As String = OpenFileDialog2.FileName
Dim links As String = IO.File.ReadAllText(link)
MsgBox(links)
WebBrowser1.Navigate(links)
You help means a lot. Thank You.
The WebBrowser Control either will show the webpage in the Control which will limit you to one page, or you can tell it to open the pages in separate windows which will open an Internet Explorer window for each link. I also used the File.ReadAllLines Method in order to get an array of the Links so that you can iterate through the Web Pages . This works for me but might not be what you are wanting.
Public Class Form1
Dim wb As New WebBrowser
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim filename As String = "C:\temp\links.txt"
Dim links As String() = IO.File.ReadAllLines(filename)
For Each link As String In links
wb.Navigate(link, True)
Next
End Sub
Public Sub New()
InitializeComponent()
Controls.Add(wb)
wb.Dock = DockStyle.Fill
End Sub
End Class
My text file called Links.txt looks like this:
www.google.com
www.msdn.com
www.bing.com

how to automatically click search button in google visual basic . net

I have a form with textbox, button and a tabcontrol.
in the button I have this code :
[Dim browser As New WebBrowser()
TabPage1.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(New Uri("http://www.google.com"))]
The code above works, but I need to be able to search from my textbox and when I click the button it will take me to google and then automatically enter's the word I searched for in my textbox and then clicks the search on google button. I tried this but it does not work. Thanks
Dim textElement. As HtmlElement = browser.Document.All.GetElementsByName("q")(0)
textElement.SetAttribute("value", textbox.text")
Dim btnElement As HtmlElement = browser.Document.All.GetElementsByName("btnG")(0)
btnElement.InvokeMember("click")
I also needed to search in Google Browser with the text the User wanted and by adding the code below to the Button Click Event it did what I wanted.
Code:
Dim sInfo As New ProcessStartInfo("https://www.google.co.in/search?q=" & TXT_Entidade.Text)
Try
Process.Start(sInfo)
Catch ex As Exception
Process.Start("iexplore.exe", sInfo.FileName)
End Try
You'll need to set focus to the textElement before clicking the button.
textElement.Focus()
Otherwise, the page won't run the search apparently.
You can see this by trying the same basic steps you've got above in the Console window. They won't work until the field has had focus (from my testing).
(I also used the mshtml type library so that the click function was directly exposed in the full code below)
Imports mshtml
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
web.Navigate("http://www.google.com")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim textElement As HtmlElement = web.Document.All.GetElementsByName("q")(0)
textElement.SetAttribute("value", TextBox1.Text)
textElement.Focus()
Dim btnElement As HTMLButtonElement =
CType(web.Document.All.GetElementsByName("btnG")(0).DomElement,
HTMLButtonElement)
btnElement.click()
End Sub
End Class
I think you can use this in your button click handler...
browser.navigate("https://www.google.co.in/search?q="+textbox.Text)
This will search Google for the text in your Text Box.
You can search without "automatically clicking the search button" and you do not have to set the value in the text element of the html. This works for me. Hope this helps.