Visual Studio - getElementsByTagName() - - html

I am brand new with Visual Basic and am looking to create a program that loads up a browser and logs you in to a specific site.
However, the username and password fields of the web site that I am attempting to log into have no id or name to pull by.
Is there a way to GetElementByClass?
Any help is appreciated. I cannot seem to find much documentation online.
Similar to:
WebBrowser1.Document.GetElementById("username").SetAttribute("Value",yourUsername)
For example, the input box I am trying to get is:
<input type="text" class="gwt-TextBox" maxlength="50" style="width: 180px;">
So far, this is what I have come up with:
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("className").ToString
'MessageBox.Show(curElement.GetAttribute("className").ToString)
' This shows classNames of gwt-TextBox and gwt-PasswordTextBox
Dim user As String = "user"
Dim pass As String = "pass"
WebBrowser1.Document.All.GetElementsByName("gwtTextBox").SetAttribute("value", user)
WebBrowser1.Document.All.GetElementsByName("gwt-PasswordTextBox").SetAttribute("value", user)
I am now seeing that SetAttribute is not a member of HtmlElement Collection.

Full Code, with help of #Jimi (https://stackoverflow.com/users/7444103/jimi)
Dim inputElementCollection As HtmlElementCollection
inputElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
Dim buttonCollection As HtmlElementCollection
buttonCollection = WebBrowser1.Document.GetElementsByTagName("button")
For Each curElement As HtmlElement In inputElementCollection
Dim inputName As String = curElement.GetAttribute("className").ToString
'MessageBox.Show(curElement.GetAttribute("className").ToString)
'gwt-TextBox
'gwt-PasswordTextBox
Dim username = uname.Text
Dim password = passwd.Text
If curElement.GetAttribute("className") = "gwt-TextBox" Then
curElement.SetAttribute("value", username)
End If
If curElement.GetAttribute("className") = "gwt-PasswordTextBox" Then
curElement.SetAttribute("value", password)
End If
Next
For Each button As HtmlElement In buttonCollection
Dim buttonName As String = button.GetAttribute("className").ToString
If button.GetAttribute("className") = "gwt-Button" Then
button.InvokeMember("click")
End If
Next

Related

Visual Basic, MySql generate id number when idnumber exist

I am trying to make a auto generated idnumber(PK) it will perform when i press autogenerate number but it still displaying the number that i just added heres my code pls help.
Dim reader as MySqlDataReader
Dim rdm As New Random
Idnumber.text = (rdm.next(0,5))
Dim comp As String
comp = "select from information where idnumber'" & idnumber.text &"'"
cmd = cmd.executereader()
if sqlds.tables(0).rows.count > 0 then
'This will perform button perform click
End if
DIm rdm as new random
Idnumber.text = (rdm.next(0,5))
Dim comp As String
comp = "select from information where idnumber'" & idnumber.text &"'"
Dim reader as MySqlDataReader = cmd.executereader()
if reader.hasrows then
Idnumber.text = (rdm.next(1,5))
end if
this method can have a duplication on your idnumber thats why rdm.next(1,5)
ill give you a tip search for guid!

Slicing a string to read a html document in VB

I was hoping someone could help me figure out why this script will not return the link names. I am trying to return a sub-string from 'http://textfiles.com/directory.html' which just writes the link names to the console, but I am struggling. The main problem - as far as I can see - is in the 'do until' loop. The working code outputs the html text to the console more for my sake than anything else (it does this successfully), but this feature may also help you guys understand the total picture I am facing. Maybe after seeing the code/ understanding my goal you guys can see where I am going wrong AND/OR suggest a better method for achieving my goal. Thanks a ton!
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim line As String = ""
Dim lowBound As String = "<a href="""
Dim highBound As String = """>"
Console.WriteLine("Grab link names from textfiles.com")
Console.WriteLine("")
Dim siteName As String = "http://textfiles.com/directory.html"
Dim tmpString As StringBuilder = New StringBuilder
My.Computer.Network.DownloadFile(siteName, "C:\~\VisualStudio\BeginnerPractice\TextFileDotCom_GrabLinkNames\TextFileDotCom_GrabLinkNames\bin\debug\directory.html", False, 500)
Dim myReader As StreamReader = New StreamReader("C:\~\VisualStudio\BeginnerPractice\TextFileDotCom_GrabLinkNames\TextFileDotCom_GrabLinkNames\bin\debug\directory.html")
While Not IsNothing(line)
line = myReader.ReadLine()
If Not IsNothing(line) Then
tmpString.Append(line)
End If
End While
Dim pageText As String = tmpString.ToString
Console.WriteLine(pageText)
Dim intCounter As Integer = 1
Do Until intCounter >= Len(pageText)
Dim checkSub As String = Mid(pageText, intCounter + 1, (Len(pageText) - intCounter))
Dim positLow As Integer = InStr(checkSub, lowBound)
Dim positHigh As Integer = InStr(checkSub, highBound)
If (positLow > 0 And positHigh > 0) And positLow < positHigh Then
Dim indexLow As Integer = checkSub.IndexOf(lowBound)
Dim indexHigh As Integer = checkSub.IndexOf(highBound)
Dim foundLink As String = checkSub.Substring(indexLow + Len(lowBound), indexHigh - Len(highBound))
Console.WriteLine(foundLink)
intCounter = intCounter + (Len(lowBound) + Len(highBound) + Len(foundLink) - 1)
Else
intCounter = Len(pageText)
End If
Loop
Console.ReadLine()
myReader.Close()
My.Computer.FileSystem.DeleteFile("C:\~\VisualStudio\BeginnerPractice\TextFileDotCom_GrabLinkNames\TextFileDotCom_GrabLinkNames\bin\debug\directory.html")
End Sub
End Module

VB.net Get text/string from html element

I'm having major trouble trying to get bits of elements and returning the strings.
I have a few exmaples of trying to get the strings and what not but failing hard.
HTML Phrasing is difficult for me to do so help would be appreciated.
Explantion of what I need:
I need to get the strinsg of different elements off this site when entering a IP
http://www.ip-tracker.org/
I need pretty much all the details but into labels or text boxes.
Or this with xml phrasing
http://ip-api.com/xml/8.8.8.8
So here is the exmaple that i've used so far but haven't got far with it.
Exmaple 1:
Dim client As New WebClient
Dim ip As String
Dim city As String
Dim Region As String
Private Function GetIp()
Try
Dim Page As String = client.DownloadString("http://www.ip-tracker.org/locator/ip-lookup.php?ip=82.16.38.43/")
ip = Page.Substring(Page.IndexOf("IP Address:") + 80)
ip = ip.Substring(0, city.IndexOf(" </td") + 30)
TextBox2.Text = ("IP Address: " + ip)
Catch ex As Exception
city = "Unable to lookup"
End Try
Return 0
End Function
To call it:
getViews()
Try xml linq
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Dim url As String = "http://ip-api.com/xml/8.8.8.8"
Sub Main()
Dim query As XElement = XElement.Load(url)
Dim status As String = query.Element("status").Value
Dim country As String = query.Element("country").Value
Dim region As String = query.Element("region").Value
Dim regionName As String = query.Element("region").Value
Dim city As String = query.Element("city").Value
Dim zip As String = query.Element("zip").Value
Dim lat As Double = query.Element("lat").Value
Dim lon As Double = query.Element("lon").Value
Dim timezone As String = query.Element("timezone").Value
Dim isp As String = query.Element("isp").Value
Dim org As String = query.Element("org").Value
Dim _as As String = query.Element("as").Value
Dim subQuery As String = query.Element("query").Value
End Sub
End Module

VBA logging in to website with 2 step login

as title says i am having an error at htdoc.all.verificationcode.Value = otp
it says Run-time error '438':
Object doesn't support this property or method
i have spent an afternoon trying to find what is wrong with it and i really hope that you guys could help me out.
Dim HTMLDoc As HTMLDocument
Dim htdoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub login()
Dim username As Range
Dim password As Range
Dim otp As Range
Dim myValue As Variant
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
MyURL = "XXXXXXXXXXXXXXXXXXXXXX"
Set MyBrowser = New InternetExplorer
MyBrowser.silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Set username = Range("B1")
Set password = Range("B2")
Set otp = Range("B3")
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.UserId.Value = username
HTMLDoc.all.password.Value = password
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
MyURL2 = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
MyBrowser.silent = True
MyBrowser.navigate MyURL2
MyBrowser.Visible = True
myValue = InputBox("Enter OTP")
Range("B3").Value = myValue
Application.Wait (Now + TimeValue("0:00:10"))
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set htdoc = MyBrowser.document
htdoc.all.verificationcode.Value = otp
For Each MyHTML_Element In htdoc.getElementsByTagName("input")
If MyHTML_Element.Type = "btnobj" Then MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
HTML in the webpage for the form is
<input name="verficationcode" id="verficationcode" type="password" size="18" maxlength="16" autocomplete="off">
<input name="btnobj" class="inputbutton" id="submitButton" onclick="checkSubmit(this);" type="button" value="Submit">
OK. That looks like a good start. You are going to a page, inputting some values and clicking Submit>. You have not set MyBrowser.visible to False so I'm assuming that you can see this going on.
The first thing you need is to wait while MyBrowser loads the page after you .click submit>. You will need to wait every time you go to a new page; My favorite is to put all of the waiting into a single line like this.
MyHTML_Element.Click
Do While (MyBrowser.Busy Or MyBrowser.readyState <> READYSTATE_COMPLETE): DoEvents: Loop
When you have that new page, you need to locate your OTP and store it in a variable. Look at the HTML behind the new page. You might see something like this:
<div id='One_Time_Password'>
47665489_29751678
</div>
You can put this into a variable you have previously declared like this.
dim myOTP as string
myOTP = MyBrowser.getElementsById("One_Time_Password").innerText
Now you have the password stored and you can do something with it.

Invoking a click on a class which is only used once

I'm trying to use a loop and I'm getting freezing but no full crash.
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("DIV")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
curElement.InvokeMember("click")
Next
In my case there should only be one div class name and I just want to invoke a click on it.
edit-improved formatting
I found a workaround like so
For Each h As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
If Not Object.ReferenceEquals(h.GetAttribute("className"), Nothing) AndAlso h.GetAttribute("className").Equals("numeroCustomer") Then
h.InnerText = loginid
Exit For
End If
Next
Hope this helps anybody with a similar problem.