VBA auto login unsuccessful Error 91 - html

Basically, I would like to auto login a website, i could find the id= "username" so i could use IE.Document.getElementById("username").value = "xxxxxxx" However, it does not work since there is no value input on Dom. Instead, when i try to edit on HTML DOM by creating new attribute - Value = "xxxxxxx", it shows the username input on website. I wonder if i could transfer this into vba and finish the auto login. Thanks a lot! Error 91 shown regarding to this case
Sub Nomuralogin()
Dim IE As InternetExplorer
Dim Stockcode As String
Dim Stocktext As String, Textchange As String
Dim HTMLDoc As MSHTML.HTMLDocument
Dim IEField As HTMLInputElement
Dim i As Integer, nAsset As Integer
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.nomuranow.com/portal/site/nnextranet/en/#curtain- login"
IE.Visible = True
Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set HTMLDoc = IE.Document
Set IEField = HTMLDoc.getElementById("username")
IEField.Value = "abc#gmail.com"
'HTMLDoc.all.item("username").value = "abc#gmail.com"
Application.Wait Now + TimeValue("00:00:03")
Application.DisplayAlerts = False
IE.Quit
Set IE = Nothing
End Sub
Pic 1 shows there is no Value input:
Pic 2 shows I have created property and added a value input:

Related

Entering text into webpage searchbox

I am trying to enter text into a searchbox and am running into different errors. Below is my code, can anyone point out to me where i have gone wrong?
Sub GetHTMLDocument()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "http://shopee.sg"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set HTMLInput = HTMLDoc.getElementsByClassName("shopee-searchbar-input__input")
HTMLInput.Value = "Excel VBA"
End Sub
I suggest try to make a test with the code sample below may help to solve the issue.
Sub demo()
Dim URL As String
Dim IE As Object
Dim element As HTMLInputElement
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "YOUR WEB PAGE ADDRESS HERE" 'Add your site address here....
IE.navigate URL
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 2, Now)
Set element = IE.document.querySelector("[class='shopee-searchbar-input__input']")
element.removeAttribute ("aria-label")
element.removeAttribute ("placeholder")
element.Value = "abc"
element.FireEvent ("OnChange")
Set IE = Nothing
End Sub
Output:
Further, you can modify the code example as your requirements.

Activating a search button via .Click

I'm using IE11 with the HMTL Object Library and Internet Controls references activated.
There's no element ID on the button but am able to use ie.Document.getElementsByClassName by adding some html and xml declarations thanks to this post.
I'm taking a name and city, state from Excel and plugging it into the website then clicking the search button.
This is where my error occurs.
Run-time error '438': Object doesn't support this property or method.
HTML:
VBA:
Option Explicit
Sub HGScrape()
'Application.ScreenUpdating = False
'we define the essential variables
Dim ie As Object
Dim my_url As String
Dim SearchButton, NameBox, AddressBox
Dim ele As Object
Dim x As Integer
Dim y As Integer
Dim IsOdd As Integer
Dim html_doc As Object 'HTMLDocument
Dim xml_obj As Object 'MSXML2.DOMDocument
my_url = "https://www.healthgrades.com/"
'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
Set ie = New InternetExplorer
ie.Visible = True 'False ''''''''''''''''''''''
ie.Navigate my_url '"13.33.74.92" '("https://www.healthgrades.com/")
While ie.ReadyState <> 4
DoEvents
Wend
Set NameBox = ie.Document.getElementById("search-term-selector-child")
NameBox.Value = ActiveSheet.Range("A2")
Set AddressBox = ie.Document.getElementById("search-location-selector-child")
AddressBox.Value = ActiveSheet.Range("B2")
Set html_doc = CreateObject("htmlfile")
Set xml_obj = CreateObject("MSXML2.XMLHTTP")
xml_obj.Open "GET", my_url, False
xml_obj.send
html_doc.body.innerHTML = xml_obj.responseText
Set SearchButton = ie.Document.getElementsByClassName("autosuggest_submiter") 'id of the button control (HTML Control)
SearchButton.Click
While ie.ReadyState <> 4
DoEvents
Wend
I condensed your code a bit. You really do not need to set every element to a variable. This just wastes resources in the long run.
Use the ClassName submiter__text to grab your submit button, and it's index of 0.
Sub HGScrape()
Const sURL As String = "https://www.healthgrades.com/"
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate sURL
While .Busy Or .ReadyState < 4: DoEvents: Wend
.Document.getElementById("search-term-selector-child"). _
Value = ActiveSheet.Range("A2")
.Document.getElementById("search-location-selector-child"). _
Value = ActiveSheet.Range("B2")
.Document.getElementsByClassName("submiter__text")(0).Click
While .Busy Or .ReadyState < 4: DoEvents: Wend
End With
End Sub
"..Why was the "submitter_text" class the correct one?"
The best way to explain it is to show you. If you are unsure what selection to make, then right-click the element and choose "Inspect Element" and look around the highlighted line.

getElementById innerHTML Runtine error 91

I am trying to extract the text from an html line (shown below) using excel vba.
<textarea class="textareaMod task-title" id="task-description">software Developemnt</textarea>
I am getting Runtime error 91 every time the code runs. Below is a part of my code. Any help to resolve this is much appreciated.
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate URLstr
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to connect to the source..."
DoEvents
Loop
Set html = ie.document
Set ie = Nothing
Application.StatusBar = ""
Dim title, header As String
title = html.getElementById("task-description").innerHTML
Activesheet.Range("B2").Value = title
Thanks SIM and Cindy ! This worked for me:
Dim Title As Object
Dim TitleTxt As String
Set Title = html.getElementById("task-description")
TitleTxt = Title.innerText
Activesheet.Range("B2").Value = Title

Excel VBA code to click web button

Need help how to create excel vba code for this
I'll be needing the codes so I can complete my macro.
Thanks in advance
First, you will need to create a reference to:
Microsoft Internet Controls
Microsoft HTML Object Library
In VBE, click Tools > References
Sub clickLink()
Dim ie As New InternetExplorer, Url$, doc As HTMLDocument
Url = "http://UrlToYourLink.com"
With ie
.navigate Url
Do While .Busy Or .readyState < READYSTATE_COMPLETE
DoEvents
Loop
doc = .document
.Visible = True
End With
Dim myBtn As Object
Set myBtn = doc.getElementsByClassName("button rounded")(0)
myBtn.Click
End Sub
The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.
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
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library
Microsoft internet controls are a great way to do this, but if you aren't allowed to add new references, here is another way to go about web scraping.
This methode ain't as 'clean' as Microsoft internet controls and HTML object but it gets the job done.
Sub GoogleSearch()
Dim ie As Object
Dim objSearchBnt As Object
Dim objCollection As Object
Dim i As Integer
'initialize counter
i = 0
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'navigate to the url
ie.navigate "Www.google.com"
'Statusbar shows in the buttom corner of excel
Application.StatusBar = "Loading, please wait..."
'Wait until page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Store all the elements with input tag
Set objCollection = ie.Document.getElementsByTagName("input")
'Go through all input elements
While i < objCollection.Length
'input search field
If objCollection(i).Name = "q" Then
objCollection(i).Value = "Hello World"
End If
'search button
If objCollection(i).Type = "submit" Then
Set objSearchBnt = objCollection(i)
End If
i = i + 1
Wend
objSearchBnt.Click
'Clean up
Set objSearchBnt = Nothing
Set objCollection = Nothing
Set ie = Nothing
'Give excel control over the status bar agian
Application.StatusBar = ""
End Sub

Getting information from HTML page via VBA

From VBA, I am trying to access to the "username" cell from a web page so that I could type in the appropriate username.
The problem is that in the HTML code from the page we have more than one element with the same name which is "LOGON_USERID" and I can't figure out how to access to the right one.
As you can see on the image "part of the HTML code", the line I'm trying to access to is the highlighted one, but there are also 2 other elements which have the same name above it.
part of the HTML code
I tried lots of different ways (using different methods or variable types etc), but since I'm not familiar with HTML I can't manage to get what I want.
Sub Pum()
Dim ie As New InternetExplorer
'Dim IEDoc As IHTMLElementCollection
Dim IEDoc As HTMLDocument
Dim name As Object
Dim nameList As HTMLInputElement
Dim WRONGS As DispHTMLElementCollection
Dim Elems As HTMLElementCollection
Dim i As Integer
ie.navigate "thewebsiteinquestion"
ie.Visible = False
WaitIE ie
Set IEDoc = ie.document
'MsgBox IEDoc.DocumentElement.
'Elems = IEDoc.getElementsByTagName("INPUT")
MsgBox TypeName(IEDoc.getElementById("LOGON_USERID").all)
Set Elems = IEDoc.getElementById("LOGON_USERID")
'For i = 0 To 5
MsgBox Elems.Length
'Next i
For Each name In Elems.Children
MsgBox name.nodeName
MsgBox name.Attributes
MsgBox name.all
Next
'If ((NameStr Isnot Nothing And (NameStr.Length <> 0)) Then
'If NameStr = "LOGON_USERID" Then
'If TypeName(IEDoc.all("LOGON_USERID")) = "HTMLInputElement" Then
'MsgBox TypeName(IEDoc.all("LOGON_USERID"))
'Set names = IEDoc.all.Item("text")
'TypeName (InputUsernameTextzone)
'Dim Question As IHTMLElement
'Question = InputUsernameTextzone.parentElement
'MsgBox TypeName(InputUsernameTextzone.parentElement.getAttribute("name"))
'InputUsernameTextzone.parentElement
'CELLULE.value = "qtc2464"
WaitIE ie
Set ie = Nothing
Set IEDoc = Nothing
End Sub
I tried two other similar codes using different methods but I still have no results. Hopefully you can help me.
If you need more information, let me know.
The other two input elements are of different type (they are hidden) so you could use querySelector with attribute type=text to find your desired element.
Dim userid As HTMLInputElement
Set userid = IEDoc.querySelector("input[name='LOGON_USERID'][type='text']")
If Not userid Is Nothing Then
' Continue with user id element
Else
MsgBox "LOGON_USERID not found on the page"
End If
I am a newbie at this but if this could help anyone, here's the simplified version of the macro I made :
Sub Access_Puma()
Dim ie As New InternetExplorer
Dim IEDoc As HTMLDocument
Dim userid As HTMLInputElement
Dim userpwd As HTMLInputElement
ie.navigate "thewebsitetoaccess"
ie.Visible = True
WaitIE ie
Set IEDoc = ie.document
Set userid = IEDoc.querySelector("input[name='LOGON_USERID'][type='text']")
If Not userid Is Nothing Then
userid.value = "myusername"
Else
MsgBox "LOGON_USERID not found on the page"
End If
Set userpwd = IEDoc.querySelector("input[name='LOGON_PASSWD'][type='password']")
If Not userpwd Is Nothing Then
userpwd.value = "mypassword"
Else
MsgBox "LOGON_PASSWD not found on the page"
End If
End Sub