getElementById innerHTML Runtine error 91 - html

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

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.

VBA auto login unsuccessful Error 91

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:

Pull value from website (HTML div class) using Excel VBA

I'm trying to automate going to a website and pulling the ratings from several apps.
I've figured out how to navigate and login to the page.
How do I pull the element - the number "3.3" in this case - from this specific section into Excel.
Being unfamiliar with HTML in VBA, I got this far following tutorials/other questions.
Rating on website and the code behind it
Sub PullRating()
Dim HTMLDoc As HTMLDocument
Dim ie As InternetExplorer
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.appannie.com/account/login/xxxxxxxxxx"
Set ie = New InternetExplorer
ie.Silent = True
ie.navigate sURL
ie.Visible = True
Do
'Wait until the Browser is loaded
Loop Until ie.readyState = READYSTATE_COMPLETE
Set HTMLDoc = ie.Document
HTMLDoc.all.Email.Value = "xxxxxxxxx#xxx.com"
HTMLDoc.all.Password.Value = "xxxxx"
For Each oHTML_Element In HTMLDoc.getElementById("login-form")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Dim rating As Variant
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
Range("A1").Value = rating
'ie.Refresh 'Refresh if required
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
The code below will let you extract text from first element with class name "rating-number ng-binding" in HTML document. By the way GetElementsByClassName is supported since IE 9.0. I use coding compatible also with older versions in my example.
Dim htmlEle1 as IHTMLElement
For Each htmlEle1 in HTMLDoc.getElementsByTagName("div")
If htmlEle1.className = "rating-number ng-binding" then
Range("A1").Value = htmlEle1.InnerText
Exit For
End if
Next htmlEle1
While Ryszards code should do the trick if you want to use the code you have already written then here is the alterations I believe you need to make.
For Each oHTML_Element In HTMLDoc.getElementById("login-form")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
'Need to wait for page to load before collecting the value
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim rating As IHTMLElement
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
'Need to get the innerhtml of the element
Range("A1").Value = rating.innerhtml

VBA to fetch html URL from webpage

I have created Macro which can read all the HTML of provided URL, however I want to fetch all the url from that HTML.
Sub GetHTML_Click()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim j As Integer
Set ie = New InternetExplorer
ie.Visible = True
url = Cells(1, 2)
ie.navigate url
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to website ..."
Loop
Application.StatusBar = " "
Set html = ie.document
'Dim htmltext As Collection
Dim htmltext As String
htmltext = html.DocumentElement.innerHTML
'Need to add iURL
Dim htmlurl As IHTMLElement
For Each htmlurl In hmtltext
iurl = htmlurl.toString
Cells(j, 1).Value = CLng(iurl)
j = j + 1
Next
End Sub
I tried to code this to fetch the URLs however its giving "Object Required error"
can anyone please help to modify this macro which will help me to fetch all the URL from HTML page.
I am using www.mini.in website for testing.
Mayur.
Try this :
Dim ie As Object
Dim html As Object
Dim j As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
URL = "google.com"
ie.Navigate URL
Do While ie.ReadyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to website ..."
Loop
Application.StatusBar = " "
Set html = ie.Document
'Dim htmltext As Collection
Dim htmlElements As Object
Dim htmlElement As Object
Set htmlElements = html.getElementsByTagName("*")
For Each htmlElement In htmlElements
If htmlElement.getAttribute("href") <> "" Then Debug.Print htmlElement.getAttribute("href")
Next

how to click a link in IE using excel VBA

I am trying to wrote some code that will lookup a word in an excel cell, the search for that word and, most importantly, click on the link that returns.
I have the search part down, but it's the click on the link I'm struggling with.
the HMTL extract is -
<span>
<div class="searchResult webResult ">
<div class="resultTitlePane">
<h3>
<a class="outbound" href="whatever" target ="" rel="nofollow" rev="lots of
text">..</a>
</h3>
</div>
I've not typed out the href and rel text, but I just want to be able to click the link that's returned and follow through to that site.
Any help please?
this is my code -
Sub test()
Dim ie As InternetExplorer
Dim RegEx As RegExp, RegMatch As MatchCollection
Dim MyStr As String
Dim pDisp As Object
Dim dtStartTime As Date
Set ie = New InternetExplorer
Set RegEx = New RegExp
Dim iedoc As Object
SearchEng = "http://easysearch.org.uk/search/?s="
LastRow = Range("A1").End(xlDown).Row
Do Until i = LastRow + 1
SearchMe = Range("A" & i)
ie.Navigate SearchEng & SearchMe
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
MyStr = ie.document.body.innerText
Set RegMatch = RegEx.Execute(MyStr)
If RegMatch.Count > 0 Then
ie.Navigate RegMatch(0)
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
ie.Visible = True
Set iedoc = ie.document
''NEED TO ADD SOMETHING HERE TO CLICK LINK''
End If
i = i + 1
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
Loop
Set RegEx = Nothing
ie.Quit
Set ie = Nothing
End Sub
In your code, you can replace you placeholder: ''NEED TO ADD SOMETHING HERE TO CLICK LINK'' with:
iedoc.getElementsByClassName("resultTitlePane")(0).getElementsByTagName("a")(0).Click
In response to OP's comment of readystate not completing:
You can continue to use your readystate loop, and then loop the actual object until it becomes available, then click it:
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
ie.Visible = True
Set iedoc = ie.document
' ^ ^ ^ ^ Already part of your code
Dim myBtn As Object
Set myBtn = Nothing
' Loop object while it's Nothing
On Error Resume Next
Do While myBtn Is Nothing
DoEvents
set myBtn = iedoc.getElementsByClassName("resultTitlePane")(0).getElementsByTagName("a")(0)
Loop
On Error GoTo 0
myBtn.Click