Unable to to locate and click a submit button using mshtml.HTMLInputElement - vb.net

Below is is an HTML form and below that, is a vb procedure, "LoginExamp" that enters in the username and password. I am unable to locate the button however and click it since it does not seem to show up as a mshtml.HTMLInputElement. "htmlInput.click()" never runs. How can I adjust the loginExamp code so that the button is clicked. Thanks for any help.
<form id="loginform" name="loginform" method="post" action="">
<input id="username" class="formfield" type="text" value="User Name" maxlength="40" name="Xusername">
<input id="password" class="formfield" type="password" onfocus="clearDefault(this)" maxlength="40" name="Xpassword">
<button class="subButton" onclick="javascript: submitform()">submit!</button>
</form>
With the below code
Public Sub loginExamp()
Dim objIE As SHDocVw.InternetExplorer
Dim htmlDoc As mshtml.HTMLDocument
Dim htmlInput As mshtml.HTMLInputElement
Dim htmlColl As mshtml.IHTMLElementCollection
Dim url As Object
url = "http://localhost/ButtonClickTest.html" 'just a test page with the loginform above
objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate(url)
.Visible = True
While .Busy = True
Threading.Thread.Sleep(2000)
End While
htmlDoc = .Document
htmlColl = htmlDoc.getElementsByTagName("INPUT")
While .Busy = True
Threading.Thread.Sleep(2000)
End While
For Each htmlInput In htmlColl
If htmlInput.name = "Xusername" Then
htmlInput.value = "theusername" 'this works
ElseIf htmlInput.name = "Xpassword" Then
htmlInput.value = "thepassword" 'This works too
End If
If htmlInput.className = "subButton" Then 'This is never true
htmlInput.click() 'This line never runs
End If
Next htmlInput
End With
End Sub

I was able to finally figure out the answer to my own question. Below is the code that works. Both the username and password is entered and also the button is clicked.
Public Sub loginExamp()
Dim objIE As SHDocVw.InternetExplorer
Dim htmlDoc As mshtml.HTMLDocument
Dim htmlInput As Object
Dim url As String
url = "http://localhost/N1/ButtonClickTest.html"'test page with the loginform above
objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate(url)
.Visible = True
While .Busy = True
Threading.Thread.Sleep(2000)
End While
htmlDoc = .Document
Dim htmlColl As mshtml.HTMLFormElement = DirectCast(htmlDoc.forms.item("loginform"), mshtml.HTMLFormElement)
While .Busy = True
Threading.Thread.Sleep(2000)
End While
For Each htmlInput In htmlColl
If htmlInput.name = "Xusername" Then
htmlInput.value = "theusername"
ElseIf htmlInput.name = "Xpassword" Then
htmlInput.value = "thepassword"
End If
If htmlInput.className = "subButton" Then
htmlInput.click()
End If
Next htmlInput
End With
End Sub

Related

getElementsByName in VBA

I have a problem with entering data from Excel into a web browser using the code below. He reports error number 438 to me and I don't know how to solve it.
The error appeared here:
iframeDoc.getElementsByName("matbr").Value = Range("a2").Value
This is my full code:
Sub provera_menice()
Dim wb As Workbook
Set wb = ThisWorkbook
'Dim rgMB As Range
'Set rgMB = Application.InputBox("Izabrati MB klijenta koji se deblokira.", Type:=8)
'Dim r As Long
'Dim k As Long
'r = rgMB.Row
'k = rgMB.Column
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/duznici-pn/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Dim ieDoc As MSHTML.HTMLDocument
Set ieDoc = objIE.document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = ieDoc.frames(0).document
'in the search box put some cell value
'MB: <input class="form-control" name="matbr" type="text" tabindex="4" size="13" maxlength="8" value="">
iframeDoc.getElementsByName("matbr").Value = Range("a2").Value
'search: <input class="btn" type="submit" name="send" id="send" value="????????" tabindex="8">
iframeDoc.getElementById("send").Click
End Sub
I searched for a solution on the net, but I can't fix the error.
I try to set loop, but nothing entry.
Set x = iframeDoc.getElementsByName("matbr")
For i = 0 To x.Length
If x(i) = "matbr" Then
'in the search box put some cell value
'MB: <input class="form-control" name="matbr" type="text" tabindex="4" size="13" maxlength="8" value="">
iframeDoc.getElementsByName("matbr").Value = Range("a2").Value
'search: <input class="btn" type="submit" name="send" id="send" value="????????" tabindex="8">
iframeDoc.getElementById("send").Click
End If
i = i + 1
Next
I found a solution, thank you all!
Here is the correct code:
Sub provera_menice()
Dim wb As Workbook
Set wb = ThisWorkbook
'Dim rgMB As Range
'Set rgMB = Application.InputBox("Izabrati MB klijenta koji se deblokira.", Type:=8)
'Dim r As Long
'Dim k As Long
'r = rgMB.Row
'k = rgMB.Column
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/duznici-pn/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Dim ieDoc As MSHTML.HTMLDocument
Set ieDoc = objIE.document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = ieDoc.frames(0).document
iframeDoc.getElementsByName("matbr")(1).Value = Range("a2").Value
iframeDoc.getElementById("send").Click
Range("b4").Value = iframeDoc.getElementsByClassName("table_text cell")(5).textContent
End Sub

Href VBA clicking

I've been trying many codes to click the right button and open the page to scrape the data.
I was able to have the form filled and then click on submit, but the second part I can't seem to put to work. I want it to click on the href with has the same name that I searched on the form
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub CETIP()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www2.cetip.com.br/TitulosCRI"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.Navigate sURL
oBrowser.Visible = True
Do
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.document
HTMLDoc.all.ddlCodigoIF.Value = "08L0002118"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Err_Clear:
Resume Next
End Sub

Clicking a button in Internet Explorer

I am trying to create some VBA code that clicks a button with the following HTML:
<button class="button small btnViewAction atlas--ui-button" type="button" data-tracking-value="Action" data-tracking-label="View Action" data-tracking-## Heading ##category="Reconciliations" data-attribs-childassignmentid="661" data-attribs-reconciliationid="145870" data-attribs-assignmenttype="A" data-attribs-assignmentid="1223" value="undefined" data-columnname="action">Edit</button>
Is there a way to reference this button?
Without seeing more of the HTML I can't say this will work for sure, but it should give you a good guideline for what you can do!
Make sure you update ie.navigate to your site.
This is a late binding example, you can also set a reference to Microsoft Internet Controls.
Sub clickButton()
Dim ie As Object
Dim Btn As Object
'Late Binding
Set ie = CreateObject("InternetExplorer.Application")
On Error GoTo Catch
ie.Visible = True
ie.navigate "https://yourwebsite.com/"
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
'LOOP EACH CLASS ELEMENT
For Each Btn In ie.Document.getElementsByClassName("button")
If Btn.innertext = "Edit" Then
Btn.Click
End If
Next Btn
'CLOSE INSTANCE OF IE
Catch:
ie.Quit
Set ie = Nothing
End Sub
Here is a very generic sample of how to do this.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
Try the code above and see if you can implement it for your specific situation. If you need a nudge, post back, with additional requirements.

Login into a Website

I have read a number of posts related to this topic, tried a number of different methods and still can't get it to work.
The macro is working for the user name and password but login not working..
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub RepsolLogin()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim form As HTMLFormElement
MyURL = "https://login.repsol.com/es/Landing/AuthnPage?returnUrl=https://www.repsol.com/es_es/"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
DoEvents
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
Do While HTMLDoc.getElementById("gigya-login-form") Is Nothing
DoEvents
Loop
Set form = HTMLDoc.getElementById("gigya-login-form")
form.all.UserName.Value = "xxx#xxx.com" 'Enter your email id here
form.all.Password.Value = "password" 'Enter your password here
form.submit
End Sub
Can anyone have a look an tell me how I must have the macro?

How to use VBA to click on button in webpage after you have logged in?

I am trying to click on a button on a webpage that I login to with VBA. I have already logged in and now I want to continue the process and click on another button. Here is what I have so far
Sub MyGmail()
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://www.programworkshop.com/6.0.0.0/login/login.aspx?skin=default&lang=enu"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.Navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.Document
HTMLDoc.all.Login.value = "something#something.com"
HTMLDoc.all.pass.value = "Password"
HTMLDoc.all.btnlogin.Click
This next line is where I hit a dead end
HTMLDoc.all.sd5.Click
Here is the code from the website
<a id="sd5" class="nodeSel" href="javascript:treeAction(strURL5)" onmouseover="window.status='Examinees';return true;" onmouseout="window.status='';return true;" onclick="javascript: d.s(5);">Examinees</a>
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
HTMLDoc.all.btnlogin.Click
While MyBrowser.Busy
DoEvents
Wend
Set HTMLDoc = MyBrowser.Document
HTMLDoc.all.sd5.Click