I'm trying to make a webscraping program in Visual Studio 2019 using VB.Net Framework but I'm running into an issue. I have the below code to locate an open IE window so that I can manipulate it. The issue I'm running into is when I use IE.Document. the list that appears does not show getelementsbyid. Here is a snippet of my code, it's able to find the appropriate IE window and bring it into focus but I need to be able to manipulate the elements on the page.
I have also added the Com References
Microsoft HTML Object Library
Microsoft Internet Control
Dim SWS as new SHDocVw.ShellWindows
Dim IE as SHDocVw.InternetExplorer
For Each IE in SWS
If IE.locationName = "XXXXXXXX" Then Exit For
NExt
'To get focus on IE window
IE.Visible = False
IE.Visible = True
IE.Document.
I would appreciate any insight since I'm at a loss as to why getelementbyid won't show.
As an alternative, you can try to refer to the code example below that may help you to get/ set values.
If you want to set data then refer to the line below.
IE.Document.All.Item("fname").Value = "ABC"
If you want to fetch data then refer to the line below.
Console.WriteLine(IE.Document.All.Item("fname").Value)
Related
Below is the error I'm getting when entering the codeI'm new to VBA but I'd like to create a macro that returns the top 5 search results of the item listed in cell A15 using either google chrome or microsoft edge? I tried adding in the code below and got an error.
VBA can only automate the Internet Explorer browser. So if you want to use google chrome or Microsoft edge then this approach will not work for you.
Below is the sample code that populates Google search results to a worksheet.
Option Explicit
Public Sub GetLink()
Dim ie As New InternetExplorer
Dim url As String
url = "https://google.co.uk/search?q=" + Sheet1.Range("A2").Value
With ie
.Visible = True
.navigate url
While .Busy Or .readyState < 4: DoEvents: Wend
Sheet1.Range("B2").Value = .document.querySelector("#search div.r [href*=http]").href
Sheet1.Range("C2").Value = .document.querySelector("#search div.r [href*=http]").innerText
.Quit
End With
End Sub
Output:
Reference:
How to get the first search result link of a google search using VBA?
Below is another helpful thread. Its solution uses the XMLHTTP object.
Using VBA in Excel to Google Search in IE and return the hyperlink of the first result
Further, you can try to check the above example and try to modify the sample as per your own requirements.
VBA code to interact with specific IE window that is already open
Above is a thread to find and go to an already open instance of IE using shell applications in VBA. AFTER I found the open IE instance I am looking for, I need to query the tables from that IE page without using it's URL. The reason that I cannot use it's URL is that this IE page is a generic 'result' page that opens in a separate window after doing a search on the main website, so if I use the URL of the result page, which is: https://a836-acris.nyc.gov/DS/DocumentSearch/BBLResult, it will return an error. Are there any other methods that allow querying tables without using URL connections, like a "getElements" for tables?
K.Davis, Tim William: you are correct in your assumptions. The first part of my code/project opens up a search page: objIE.navigate "https://a836-acris.nyc.gov/DS/DocumentSearch/BBL" and through it I submit a search form. The second part (outlined above in the first paragraph) opens up a result page (pop-up). I am trying to automate the retrieving of the tables from that page. I tried using QueryTables.Add method, the way I am familiar with to connect to the data/webpage requires an URL. If I use the URL from the result page it returns an error, thus I am looking for suggestions/help on how I could otherwise connect. That said I am able to retrieve elements of the page using 'getElements' method but not able to query tables. There are other ways to connect to the data source using the QueryTables.Add method, see, https://learn.microsoft.com/en-us/office/vba/api/excel.querytables.add but I am not familiar with these other methods. Hope this clarifies a bit.
I haven't experienced a problem with this as although you have an intermediate window the final IE window resolves to being the main IE window with focus. I was able to grab the results table with the following code using the indicated search parameters:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer
With IE
.Visible = True
.navigate "https://a836-acris.nyc.gov/DS/DocumentSearch/BBL"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("option[value='3']").Selected = True
.querySelector("[name=edt_block]").Value = 1
.querySelector("[name=edt_lot]").Value = "0000"
.querySelector("[name=Submit2]").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Dim hTable As HTMLTable
Set hTable = .document.getElementsByTagName("table")(6)
'do stuff with table
.Quit
End With
End Sub
You can copy a table via clipboard. Any tick windings appear in the right place but as empty icons.
For clipboard early bound go VBE > Tools > References > Microsoft-Forms 2.0 Object Library.
If you add a UserForm to your project, the library will get automatically added.
Dim clipboard As DataObject
Set clipboard = New DataObject
clipboard.SetText hTable.outerHTML
clipboard.PutInClipboard
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).PasteSpecial
Late bound use
Dim clipboard As Object
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
First I'm very new to trying to automate IE via Excel VBA.
That being said, I'm working to automate a login to a company-specific webpage (only accessible to our employees). The goal is to automate the login (employee number, password and click Login). I find Firefox to be particularly helpful in identifying fields so that's what I'm using in the screenshot.
I found some code online to navigate to a webpage and enter something into a search box. I've modified that as follows (the included link is not real).
Finally to the issue. If I enter a webpage like www.google.com for example, all will execute fine. But when I change to my company link, the code freezes at the Do While and I get the error shown. So my question is why it works for a general webpage but not for my company specific one? If I comment-out that line, I still get the disconnected error when debugging. Assuming that issue is an easy one to resolve, have I also properly identified the field?
Hopefully I've included enough info for you. If not, please let me know what else may be required. Thanks in advance for your help!
Error
'start a new subroutine called SearchBot
Sub SearchBot()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
'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 "http://sampletext.asp"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'enter value in the employee number box
objIE.document.getElementByName("txtEmployeeNum").Value = "123456"
Employee Number
Correct name of the method is getElementsByName.
You also want to operate on element of collection returned by this method, not whole collection. Using (0) index will allow to work on 1st element of collection.
Change:
objIE.document.getElementByName("txtEmployeeNum").Value = "123456"
to:
objIE.document.getElementsByName("txtEmployeeNum")(0).Value = "123456"
With such corrected code, you should step through code with F8 in VB Editor. For example by hovering over, see if objIE.Busy ever gets FALSE and especially if objIE.readyState ever reaches 4 - if only 3, try objIE.readyState < 3 instead.
EDIT:
Try replacing:
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
with:
Dim objIE As Object
Set objIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
You may also need to change objIE.Navigate with objIE.Navigate2
I am encountering an
Object doesn't support this property or method
Runtime error in excel vba.
Note : The code works fine in my laptop.This error happened when I migrated the code to my desktop pc.
Below is the UPDATED code.
Dim ie As InternetExplorer
Dim htmldoc As MSHTML.IHTMLDocument
Dim ro1 As MSHTML.IHTMLElementCollection
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.navigate "url"
Set htmldoc = ie.document
Set ro1 = htmldoc.getElementById("table id").getElementsByTagName("tr")
ThisWorkbook.Sheets(1).Cells(k, j) = rows(k).Children(0).textContent
ro1(k).Children(0).textContent is the error part.
I have checked Tools->References. Microsoft Internet Controls and Microsoft HTML Object Library has been checked.
Can anyone please guide me for this ?
Try using InnerText instead of textContent and see if that works for you.
ThisWorkbook.Sheets(1).Cells(k, j) = rows(k).Children(0).InnerText
Start by adding a line Option Explicit at the top of your module, and then Debug->Compile to identify variables and objects that aren't properly declared or are being misused, Keep compiling until no more warnings.
Also, don't use the word ROWS as an object name. Rows already means something in Excel & VBA.
Here is a list of reserved words that should be avoided. (I realize it's labelled Access 2007 but the list is very similar, and it's surprisingly difficult to find an recent 'official' list for Excel VBA.)
I would like to ask you if it's possible (and how), to copy all data from an open explorer window (name or path not known) automatically to another folder? I don't think that this is possible with .bat, but maybe with VB?
Here's a trivial VBA example where I open a web page in Internet Explorer, copy all the text, and place it in a cell. Your question isn't very clear, so I don't know whether this addresses what you want to do, but it can probably help.
EDIT Upon re-reading your question I think I confused Explorer with Internet Explorer... So this may not be relevant after all. If so, I will delete this.
Dim ex As InternetExplorer
Dim hd As HTMLDocument
Dim s As String
Set ex = New InternetExplorer
With ex
.Navigate "http://donttrack.us/"
.Visible = 1
Set hd = .Document
s = hd.body.innerText
Range("A1") = s
End With