Getting object error when getting getElementById value on webpage - html

I am automating web scraping and i'm getting an object error 438 object doesn't support this property or method. in vba when i get to last line of code. If i run it on the internet explorer console, i get the value but i get error in vba. Any help?
Dim shellWins As ShellWindows
Dim IE As Object
Set shellWins = New ShellWindows
If shellWins.Count > 0 Then
' Get IE
Set IE = shellWins.Item(0)
Else
' Create IE
Exit Sub
End If
IE.Navigate "https://mywebpage.com"
While IE.Busy
DoEvents
Wend
Do Until IE.ReadyState = 4
DoEvents
Loop
Dim rtn As String
rtn = IE.getElementById("myID").getAttribute("value") << I get ERROR here

I saw your question in this comment,
is there a way to set an object to an already existing instance of IE ?
so here's what you can do to grab an already-existing IE browser:
Dim IE As Object
Set IE = GetIE("https://mywebpage.com")
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim RetVal As Object
Set objShell = CreateObject("shell.application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.document.Location
On Error GoTo 0
If sURL Like "*" & sLocation & "*" Then
Set RetVal = o
Exit For
End If
Next o
Set GetIE = RetVal
End Function
Now to the actual question. It's impossible to help you precisely without seeing HTML code or the website itself.
However, you can try either of these properties to see if they'd work (Try in the order listed here):
Dim rtn As String
rtn = IE.Document.getElementById("myID").Value
Dim rtn As String
rtn = IE.Document.getElementById("myID").innerText
Dim rtn As String
rtn = IE.Document.getElementById("myID").outerText

The method is a property of .document (HTMLDocument) object not the IE object
IE.document.getElementById("myID")
To create an IE instance:
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

Going to agree with some of the above answers. Get off IE unless its a constraint.
You're not creating an IE object at all. So you're trying to use a non-existent object.
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

First of all: Don't use IE. Firefox and Chrome are much more viable and are actually HTML comformant.
In case this doesn't fix it: Furthermore, if your script runs on startup and is declared before the HTML, it wil try search the current buffer and wont find the object since it hasn't been loaded yet. Try placing the script after the HTML declaration.
I'm sorry if I misunderstood the Code above since I don't actually speak VB ^^'

Related

How can i delete HTML element in vba excel

I am working on a code that uses VBA-Excel to navigate to a website and automate a daily task i do . Having said that , am stuck in between of trying to delete an html element . The following js code works perfect i.e. :-
var elmnt = document.getElementById("foregxyz");
elmnt.remove();
How could i make the above 2 lines of js code in vba excel ?
I tried the following but with no result . Please help , thanks in advance .
Sub DailyTask()
Dim ie As InternetExplorer
Set ie = New InternetExplorerMedium
Dim url As String
Dim elementToBeDeleted As String
url = "https://www.example.com"
ie.Visible = True
ie.Navigate url
While ie.Busy
DoEvents
Wend
ie.Document.getElementsByTagName("select")("toDOList").Value = "TEST1"
ie.Document.getElementsByTagName("select")("scheduleDate").Value = "TEST2"
ie.Document.getElementsByTagName("select")("descriptionTask").Value = "TEST3"
'Many more of data entry until submitting the form works fine .
elementToBeDeleted = ie.Document.getElementById("xyz")
Call DeleteTagById(elementToBeDeleted)
Set ie = Nothing
End Sub
Function DeleteTagById(elementToBeDeleted As String) As String
On Local Error GoTo MyError
Dim HTMLDoc As HTMLDocument
Dim Node As IHTMLDOMNode
If elementToBeDeleted = "" Then GoTo MyExit
Set HTMLDoc = New HTMLDocument
Set Node = HTMLDoc.getElementById("xyz") 'Node always equals to nothing , why ? hence i get stuck here and cant move further.
'If Node Is Nothing Then GoTo MyExit
Node.ParentNode.RemoveChild Node 'this line gives me run time error 91 object variable or with block variable not set .
MyExit:
Set Node = Nothing
Set HTMLDoc = Nothing
Exit Function
MyError:
'Handle Error
Resume MyExit
End Function

VBA - losing control of internet exploror

I have a function that loads a website prompting a user to login. This works on everyone's computers except one person and I cannot figure it out. I get a runtime error on the line Do Until ieApp.ReadyState = 4: DoEvents: Loop that reads "Automation error. The object invoked has disconnected from its clients."
Function Firewall_Login() As Boolean
Dim ieApp As Object
Dim ieDoc As Object
Dim ieTable As Object
Dim ieDocResult As Object
Dim start_time
Set ieApp = CreateObject("internetexplorer.application")
start_time = Now()
ieApp.Navigate "http://awebsite.com/"
Do While ieApp.ReadyState = 4: DoEvents: Loop
Do Until ieApp.ReadyState = 4: DoEvents: Loop 'errors here just on the one computer
Set ieDoc = ieApp.Document
...
'do some stuff
...
ieDoc.Close
Set ieDoc = Nothing
ieApp.Quit
Set ieApp = Nothing
Firewall_Login = True
End Function
Seems like the object ieApp simply loses the instance of IE and can't do anything. Anyone have any suggestions?
All users are on the same version of IE -- 11.0.9600.18816
This code is taken from my blog. The code loops through shell windows looking to match the title bar which for IE will be the url. This way the reference is reacquired. Sometimes the reference can be lost due to a change of security zone etc.
Option Explicit
'* Tools - References
'* MSHTML Microsoft HTML Object Library C:\Windows\SysWOW64\mshtml.tlb
'* SHDocVw Microsoft Internet Controls C:\Windows\SysWOW64\ieframe.dll
'* Shell32 Microsoft Shell Controls And Automation C:\Windows\SysWOW64\shell32.dll
Private Function ReacquireInternetExplorer(ByVal sMatch As String) As Object
Dim oShell As Shell32.Shell: Set oShell = New Shell32.Shell
Dim wins As Object: Set wins = oShell.Windows
Dim winLoop As Variant
For Each winLoop In oShell.Windows
If "C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE" = winLoop.FullName Then
Dim sFile2 As String
sFile2 = "file:///" & VBA.Replace(sMatch, "\", "/")
If StrComp(sFile2, winLoop.LocationURL, vbTextCompare) = 0 Then
Set ReacquireInternetExplorer = winLoop.Application
GoTo SingleExit
End If
End If
Next
SingleExit:
End Function

VBA: handling data in Document Object Model

I am currently trying to scrap data from a website using VBA. I am following this tutorial and hence my code is the following one:
Sub Foo()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://www.ishares.com/it/investitore-privato/it/prodotti/251843/ishares-euro-high-yield-corporate-bond-ucits-etf"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Set allRowOfData = appIE.document.getElementsByClassName("visible-data totalNetAssets")
Dim myValue As String
myValue = allRowOfData.Cells(1).innerHTML
MsgBox myValue
End Sub
Unfortunately there are some differences between data I want to scrap and those ones used in the example: this line
myValue = allRowOfData.Cells(1).innerHTML
is wrong according to VBA debug.
Anyone could provide me with some explanations about why that doesn't work and how am I supposed to pick the right method to scrap HTML pages?
Try the below change which will solve your issue. In brief, you will need to treat the allRowofData as a collection.
myValue = allRowOfData(0).Cells(1).innerHTML

Navigating IE object via VBA code

I have a VBA code which allows me to read/place string in HTML objects (like input boxes).
This is the example of code:
Sub ReadCheck()
Dim objlement As Object
Dim i As Integer
Dim isNothing As Boolean
'
Dim shellWins As ShellWindows
Dim ie As InternetExplorer
'Set shellWins = New ShellWindows
Set shellWins = New ShellWindows
i = shellWins.Count
'On Error Resume Next
For i = 0 To i - 1
If shellWins.Item(i).Parent = "Internet Explorer" Then
If shellWins.Item(i).Document.URL = "https://intservices.mybebpage" Then
Set ie = shellWins.Item(i)
Exit For
End If
End If
Next i
On Error GoTo 0
isNothing = ie Is Nothing
Debug.Print Format(Now, "HH:MM:SS") & " IE is nothing: " & isNothing
If isNothing Then Exit Sub
'combo box
Set objlement = ie.Document.getelementsbyname("txtboxx")
objlement (0).Value = "Some text to place"
End Sub
Due to my testing, this code works fine with 99% of cases, but sometimes it returns error 91 (Object variable or With block variable not set), in the last line.
I'm pretty sure, that I can get correct object for objelement, but it looks like there were no .Value property for it.
This is wierd, becouse I know that this code works.
In my opinion the most possible reason for this is that I should use some different properties than .Value for this.
Do you have any comments for this?

HTML Page Title in Excel VBA

Given an url, how can I get the title of the html page in VBA in Excel?
For example suppose I have three urls like :
http://url1.com/somepage.html
http://url2.com/page.html
http://url3.com/page.html
Now I need to get the title of these html pages in another column. How do I do it?
Remou's answer was VERY helpful for me, but it caused a problem: It doesn't close the Internet Explorer process, so since I needed to run this dozens of times I ended up with too many IEs open, and my computer couldn't handle this.
So just add
wb.Quit
and everything will be fine.
This is the code that works for me:
Function GetTitleFromURL(sURL As String)
Dim wb As Object
Dim doc As Object
Set wb = CreateObject("InternetExplorer.Application")
wb.Navigate sURL
While wb.Busy
DoEvents
Wend
GetTitleFromURL = wb.Document.Title
wb.Quit
Set wb = Nothing
End Function
I am not sure what you mean by title, but here is an idea:
Dim wb As Object
Dim doc As Object
Dim sURL As String
Set wb = CreateObject("InternetExplorer.Application")
sURL = "http://lessthandot.com"
wb.Navigate sURL
While wb.Busy
DoEvents
Wend
''HTML Document
Set doc = wb.document
''Title
Debug.Print doc.Title
Set wb = Nothing
if you use Selenium :
Sub Get_Title()
Dim driver As New WebDriver
debug.print driver.Title
End Sub