How do I invoke a click event with GeckoFx? - vb.net

I can't seem to invoke a click event in GeckoFX in vb.net. Here is my code:
Imports Skybound.Gecko
Public Class Form1
Sub New()
InitializeComponent()
Xpcom.Initialize(Environment.CurrentDirectory + "/xulrunner")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GeckoWebBrowser1.Navigate("https://www.google.com")
Dim Button = New GeckoButtonElement(_webBrowser.Document.getElementById("Search").DomObject)
End Sub
End Class
The problem is that it says I do not have GeckoButtonElement class. I followed this tutorial.
Can someone help me get this button to click in another way? Or is there something else I have to do?
Side question: is there a package with the latest geckofx + xulrunner for download somewhere? I can't seem to find anything newer than what was offered in the tutorial above (which is from 2012).
Thanks.

Dim button As Gecko.DOM.GeckoButtonElement = TryCast(_webBrowser.Document.GetElementById("Search"), Gecko.DOM.GeckoButtonElement)
If button IsNot Nothing Then
button.Click()
End If
Newer versions of GeckoFx available on https://bitbucket.org/geckofx
Latest version is GeckoFx 29.0 (https://bitbucket.org/geckofx/geckofx-29.0/downloads).

Related

Is there a way to input a text from textbox1.text to input box of a website? I'm using a chromium browser in Visual Basic

I'm trying to make a program in which I can input my username from my Visual basic project Textbox1 to facebook input box username. I'm using chromium from CefSharp for webbrowser. I got the id through 'view page source' which is 'email'. but what I'm not sure if my line of code is correct.
Here are my code so far:
Imports CefSharp
Imports CefSharp.Web
Imports CefSharp.JavascriptBinding
Imports CefSharp.Event
Public Class Form2
Private WithEvents browser As ChromiumWebBrowser
Sub New()
InitializeComponent()
InitializeChromium()
End Sub
Private Sub InitializeChromium()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
Dim browser As New ChromiumWebBrowser("https://www.facebook.com")
Panel1.Controls.Add(browser)
browser.Dock = DockStyle.Fill
End Sub
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
browser.ExecuteScriptAsync("document.getElementById('email').value='ansdew#gmail.com'")
End Sub
When I run this, I get the error message
Object reference not set to an instance of an object.'
The line of code which this exception
browser.ExecuteScriptAsync("document.getElementById('email').value='ansdew#gmail.com'")
I don't know if the inside of this
ExecuteScriptAsync
is correct and I need to find the correct script so I can input my username/email using my VB project.
Let me know also if I need to include some lines of code.
Thanks!

.NET 5.0 WinForms Button_Click event code not running & multiple issues

Issue
I'm trying to Debug an application form with buttons. Usually when we double click the button, the source window shows up with the click event for that button. However, in the latest version of .NET 5.0 release, there are a lot of things not working properly.
The following are multiple issues I'm facing that usually are not there and there is no explanation on Microsoft's Blog or MSDN about these issues:
When I'm adding the source-code to any button_click event, nothing is hapenning after build and debug! I tried to make it work but to no effort.
After renaming control(s) on the form, the project needs a complete rebuild to change the names of the controls in Designer.vb file. This used to be automatic in VB prior to .NET 5.0
Application.Designer.vb randonly desides to default the Me.MainForm = Global.<ProjectNameHere>.<FormNameHere> to Me.MainForm = Global.<ProjectNameHere>.Form1 & then throws an error that Form1 is not found. Why the hell this extra work? VB used to set this automatically.
Code: Button Click Events
Public Class MainWindow
Private Sub Btn_Exit_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
Private Sub Btn_MaxMin_Click(sender As Object, e As EventArgs)
If WindowState = FormWindowState.Normal Then
WindowState = FormWindowState.Maximized
Else
WindowState = FormWindowState.Normal
End If
End Sub
Private Sub Btn_Minimize_Click(sender As Object, e As EventArgs)
WindowState = FormWindowState.Minimized
End Sub
End Class
I tried Debugging and click events don't respond. I tried to rebuild solution and it doesn't work. I tried adding Refresh() after each line of code and doesn't work. I rechecked designer.vb and everything else and it doesn't work.
.NET 5.0 is broken when using Visual Basic WinForms. The code intellisense has become slow. Everything is slow and not working.
System Information
Product Version: Microsoft Visual Studio Community 2019 16.8.0
DotNET Framework: 5.0 (Released on 11 November 2020)
Operating System: Windows 10 20H2
Please help me fix these issues. No code provided because I don't know how will other devs reproduce these issues. I cannot find any similar problems in SO or anywhere else. I can't find any problem when using .NET 4.8 or other .NET versions other than 5.0 with WinForms and VB.
Looking at your button click event handler code, I see that there is no Handles Button1.Click.
In order for the code to run, it must be associated with the event by doing:
Private Sub Btn_Exit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Another option is:
Private Sub Form1_Load(sender as Object, e as EventArgs) Handles Me.Load
AddHandler btnExit.Click, AddressOf Btn_Exit_Click 'run this somewhere when the form loads
End Sub
Private Sub Btn_Exit_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
If this is helpful, great. If not, provide more of your code so we can see what's all happening.
Regards

Cannot navigate to facebook.com from WebBrowser Tool in VB.NET

I tried to navigate to www.google.com and it worked, I even tried to navigate to www.yahoo.com and it still worked!
But when I tried to navigate to www.facebook.com it doesn't show anything.
Here's the code:
Public Class Simple
Private Sub Simple_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("https:\\www.facebook.com")
End Sub
End Class
The above code worked well but when I changed the parameter with "https:\www.facebook.com" it didn't work.
I don't know what is causing this?
Maybe it can't load javascript or something (just a guess)
Output when WebBrowser1.Navigate("https:\www.google.com")
Output when WebBrowser1.Navigate("https:\www.google.com")
WebBrowser is outdated which can cause issue with javascript and other newer libraries. I have just started to used GeckoFx which uses Firefox and not dependent on IE. Use Nuget Console "Install-Package Geckofx45 -Version 45.0.34"
Here's an example how to browse Facebook via GeckoFX
Private geckoWebBrowser As Gecko.GeckoWebBrowser
Private Sub WebView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeBrowser()
End Sub
Private Sub InitializeBrowser()
Gecko.Xpcom.Initialize("Firefox")
geckoWebBrowser = New Gecko.GeckoWebBrowser()
geckoWebBrowser.Dock = DockStyle.Fill
Me.Controls.Add(geckoWebBrowser)
geckoWebBrowser.Navigate("https://www.facebook.com")
End Sub

How to find Roku IP + Port on network using SSDP search in VB.NET

I am trying to find my Roku TV on my network and apparently it needs some SSDP discovery based on Roku API help, however, I am unable to search for my device with any of the Nuget libraries.
I came across ssdpradar and was able to install the Nuget package for Visual Studio (VB.NET) through Visual Studio 2017 community release. However, I am not able to find any documentation on how to use it.
Any advice would be helpful.
Solution:
I found a solution but not with ssdpradar and rather RSSDP. After you add the nugget in your project you can use the following line of code to get all devices and then find the Roku location (ip+port) from that list.
Imports Rssdp
For Each founddevice As DiscoveredSsdpDevice In founddevices.Result
If founddevice.Usn.Contains("roku:ecp") Then
Rokulocation = founddevice.DescriptionLocation.ToString()
Exit For
End If
Next
I was able to successfully use a library called RokuDotNet recently. It's written in C# but you could load it as a project in your solution and reference it from VB.NET.
This is roughly the way I used it:
Imports RokuDotNet.Client
Public Class Form1
Private _discoveryClient As RokuDeviceDiscoveryClient
Public Sub New()
_discoveryClient = New RokuDeviceDiscoveryClient
AddHandler _discoveryClient.DeviceDiscovered, AddressOf DiscoveryHandler
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
_discoveryClient.DiscoverDevicesAsync()
End Sub
Private Async Sub DiscoveryHandler(sender As Object, e As DeviceDiscoveredEventArgs)
If InvokeRequired Then
BeginInvoke(New Action(Sub() DiscoveryHandler(sender, e)))
Return
End If
' Get the display name for the device (if the user entered one when setting it up)
Dim deviceInfo = Await e.Device.Query.GetDeviceInfoAsync
Dim name = deviceInfo.UserDeviceName
If String.IsNullOrEmpty(name) Then
name = deviceInfo.ModelName
End If
AddDevice(e.Device, name)
End Sub
Private Sub AddDevice(device As RokuDevice, name As String)
' Your code here
End Sub
End Class
You might want to add a try/catch around the await in that async function so it can show an error if there's a network problem.

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