I wish to copy data from a table on a webpage into Excel using VBA code but didn't get anything on the Excel sheet :(.
I have tried to put together some VBA code from different sources. Here is my code:
Sub CopyWebData()
Dim IE As Object
On Error Resume Next
Application.DisplayAlerts = False
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "https://eresearch.fidelity.com/eresearch/evaluate/fundamentals/earnings.jhtml?tab=details&symbols=GOOG"
Do Until .readyState = 4: DoEvents: Loop
End With
Dim idoc As MSHTML.HTMLDocument
Dim elem As MSHTML.IHTMLElement
Set idoc = IE.document
Set elem = idoc.getElementsByClassName("layout-outer-table-width")(0).innerText
Sheets("Sheet1").Activate
Range("A1:A1000") = "" ' erase previous data
Range("A1").Select
Range("A1").Value = elem
End Sub
This is a password-protected webpage and I have logged in so I can see the webpage has been successfully pulled out by the VBA code. However, the data in the table on this webpage failed to be copied into excel - I saw nothing on the destination worksheet.
As you can see, I used code .getElementsByClassName("layout-outer-table-width") since I used Chrome's "Inspect" function to check the webpage and found that when the mouse was hovering over the statements:
...<table cellspacing="0" cellpadding="0" border="0" class="layout-outer-table-width"> == $0
<tbody>...</tbody>
</table>
part of the webpage covering the table I need was shaded. I then coded in the class name "layout-outer-table-width". However, as I said, I didn't see anything appearing on the Excel sheet.
Any instruction would be much appreciated!
If after the earnings detail table you need a different selector. I am showing a css selector for that table. Your current selector (class), and index 0, is matching on a breadcrumb (nav tree). That class is also not correct for selecting the table on the page.
.earningsHistoryTable-Cont table
I cannot test this but you may also want a timed loop for table to be present
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub RetrieveInfo()
Dim IE As InternetExplorer, hTable As Object, clipboard As Object, t As Date
Const MAX_WAIT_SEC As Long = 5
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set IE = New InternetExplorer
With IE
.Visible = True
.Navigate2 "https://eresearch.fidelity.com/eresearch/evaluate/fundamentals/earnings.jhtml?tab=details&symbols=GOOG"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#userId").Value = "xyz"
.querySelector("#password").Value = "123456"
.querySelector("form").submit
End With
While .Busy Or .readyState < 4: DoEvents: Wend
t = Timer 'timed loop for details table to be present
Do
On Error Resume Next
Set hTable = IE.document.querySelector(".earningsHistoryTable-Cont table")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While hTable Is Nothing
If Not hTable Is Nothing Then 'use clipboard to copy paste
clipboard.SetText hTable.outerHTML
clipboard.PutInClipboard
ThisWorkbook.Worksheets("Sheet1").Range("A1").PasteSpecial
End If
End With
End Sub
Related
I'm quite new to VBA so please bear with me. I've been trying to create an automation to fill in username and password and login to a site (to start with) but I've been having trouble trying to click on the submit button. Scoured the internet and learnt a whole bunch of things but I didnt find anything that seems to work. The page loads and fills in the details and nothing happens when I run the code below.
Would greatly appreciate some help with this. Thanks in advance as always!
Sub worldcheck()
Dim lastrow As Long
Dim IE As Object
Dim cel As Range
Dim post As Object
Dim ws As Worksheet
Dim element As Object
Set ws = Sheets("sheet1")
Set IE = CreateObject("internetexplorer.application")
lastrow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
IE.Visible = True
IE.Navigate "https://www.world-check.com/frontend/login/"
Do While IE.busy
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:2"))
IE.document.getElementbyID("username").Value = ws.Range("D2")
IE.document.getElementbyID("password").Value = ws.Range("D3")
IE.document.getElementbyClass("button").click
Do While IE.busy
DoEvents
Loop
End Sub
Nothing else happens? You should be getting an error message at the very least as you are trying to use a non existent method (VBA Run-time error 438 Object doesn't support this property or method) . The method is getElementsByClassName - note the s indicating it returns a collection and the ending is ClassName. You would then need to index into that collection before attempting to access the Click method
As there is only a single element with classname button you can use a faster css class selector (this is also faster than using a type selector of form; likewise, you can use the faster css equivalent of getElementById for the other two DOM elements). document.querySelector stops at the first match so is also more efficient.
Finally, rather than hard coded waits use proper page load waits as shown below:
Option Explicit
Public Sub WorldCheck()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "https://www.world-check.com/frontend/login/"
While .busy Or .readystate <> 4: DoEvents: Wend
With .document
.querySelector("#username").Value = "ABCDEF" ' equivalent to .getElementbyID("username").Value = "ABCDEF"
.querySelector("#password").Value = "GHIJKL" '.getElementbyID("password").Value = "GHIJKL"
.querySelector(".button").Click
End With
While .busy Or .readystate <> 4: DoEvents: Wend
Stop '<== delete me later
.Quit
End With
End Sub
When i insert Few words in searchbox, Its fetching related data.
I need to select first option from it.
There is one website "https://indiarailinfo.com/"
When i search "ADI" in from station box, system fetching related station having "ADI" in their name?. First option always showing very close match to it.
How can i select First Option from it using vba code
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "https://indiarailinfo.com/"
While ie.readyState <> 4: DoEvents: Wend
ie.Visible = True
ie.document.querySelector("[placeholder='from station']").Value = "ADI"
HTML Codes can be available from that site
It's Bring First Answer in Dropdown like "ADI/Ahmedabad Junction"
How can i get this answer in selected"
Kindly Suggest
Automation purists won't like using javascript to execute but I will use here for IE to trigger dropdown. If I was going pure route I would use selenium.
Option Explicit
Public Sub MakeSelection()
Dim ie As InternetExplorer, t As Date, dropdown1 As Object
Set ie = New InternetExplorer
Const MAX_WAIT_SEC As Long = 5
With ie
.Visible = True
.Navigate2 "https://indiarailinfo.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document.querySelector("[placeholder='from station']")
.Focus
.Value = "ADI"
ie.document.parentWindow.execScript "document.querySelector('[placeholder^=from]').click();"
End With
t = Timer
Do
DoEvents
On Error Resume Next
Set dropdown1 = .document.querySelectorAll(".icol span")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While dropdown1.Length = 0
If dropdown1.Length > 0 Then
dropdown1.item(0).Click
End If
Stop
.Quit
End With
End Sub
For automation purists using selenium basic
Option Explicit
Public Sub MakeSelection()
Dim d As WebDriver
Set d = New ChromeDriver
Const Url = "https://indiarailinfo.com/"
With d
.Start "Chrome"
.get Url
.FindElementByCss("[placeholder='from station']").SendKeys "ADI"
.FindElementByCss(".icol span").Click
Stop
.Quit
End With
End Sub
I am developing a web bot that scrapes the importation taxes from different countries customs website, and I have a problem retrieving the value I want from the following site : http://www.aduanet.gob.pe/itarancel/arancelS01Alias , using the test value 3303000000 next to CODIGO. The value I want to retrieve is the 6% next to "Ad / Valorem", but the table it is in has no ID properties nor class or something relevant to get directly to it or at least near to it. I have been trying to use .parent and .child methods, but without success. My code so far is as follows:
Function Peru(partida As String) As String
'Open IE
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "http://www.aduanet.gob.pe/itarancel/arancelS01Alias"
'Load sub
Cargar
'Navigate further into the website (Im using partida = 3303000000)
For Each box In objIE.document.getElementsByTagName("input")
If box.Name = "cod_partida" Then
box.Value = partida
Exit For
End If
Next
For Each boton In objIE.document.getElementsByTagName("input")
If boton.Value = "Consultar" Then
boton.Click
Exit For
End If
Next
'Get the 6% value (This part is the one I cant figure out)
End Function
This is how you can get the data from that page. It was needed to switch two iframes from that page to reach the required content.
Sub Aduanet_Info()
Dim IE As New InternetExplorer, html As HTMLDocument
Dim elem As Object, frm As Object, frm1 As Object
With IE
.Visible = False
.navigate "http://www.aduanet.gob.pe/itarancel/arancelS01Alias"
Do While .readyState <> READYSTATE_COMPLETE: Loop
Set html = .document
End With
html.getElementsByTagName("input")(0).Value = "3303000000"
html.getElementsByTagName("input")(3).Click
Application.Wait Now + TimeValue("00:00:05")
Set frm = html.getElementsByClassName("autoHeight")(0).contentWindow.document
Set frm1 = frm.getElementsByClassName("autoHeight")(1).contentWindow.document
For Each elem In frm1.getElementsByTagName("td")
If InStr(elem.innerText, "Valorem") > 0 Then MsgBox elem.NextSibling.NextSibling.innerText: Exit For
Next elem
IE.Quit
End Sub
Output:
6%
I want to download some data from a webpage (http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/) into an Excel spreadsheet.
After loading this page I have to manually choose an option from the "Código do Ativo" dropdown list, and then choose "Agenda".
Is there a way I can do it automatically via VBA?
For example: selecting "RDVT11" from the "Código do Ativo" dropdown list, selecting "Agenda" and then downloading the data from the table that will appear in the bottom part of the page?
My macro so far:
Private Sub Agenda()
Sheets("Dados").Select
Dim ProductionAddress As String
ProductionAddress = "http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/x_pu_historico_r.aspx?"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Silent = True
.Visible = True
.Navigate ProductionAddress
End With
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.document.getElementByid("ctl00_ddlAti").Value = "RDVT11|11001110111100001"
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
Set objButton = ie.document.getElementByid("ctl00_x_agenda_r")
objButton.Focus
objButton.Click
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.Quit
Set ie = Nothing
End Sub
You need to capture the request that a browser sends when the dropdown is activated. Open up Chrome dev tools and watch the network tab. You will see a POST request to sndemumclique/. This will have some headers and form data. Your code will need to basically replicate this request. Likely, not all of the header and form fields are required but there is not way to know without trying.
Here are all 3 parts. The making two selections and the writing of the table to the sheet.
Notes:
① Making first selection:
To make the RDVT11 selection, I first use the Id of the dropdown to capture the element in a variable with:
Set a = .document.getElementById("ctl00_ddlAti")
Next, I loop the drop down options, using a.getElementsByTagName("Option") to generate the collection which I loop over. When the target selection text is found, I set that option to Selected and exit the loop.
For Each currentOption In a.getElementsByTagName("Option")
If InStr(currentOption.innerText, optionText) > 0 Then
currentOption.Selected = True
Exit For
End If
Next currentOption
② Making Agenda selection:
I then target the agenda option of Sobre e emissão by its id and click it and wait for a refresh of the page:
.document.getElementById("ctl00_x_agenda_r").Click
While .Busy Or .readyState < 4: DoEvents: Wend
③ Getting the table and writing to sheet:
I then target the table that is loaded by its id. This is done within a loop to ensure the table is present:
Do: On Error Resume Next: Set nTable = .document.getElementById("aGENDA"): On Error GoTo 0: DoEvents: Loop While nTable Is Nothing
I finally, loop the rows and columns in the table, writing out to the Activesheet.
Code:
Option Explicit
Public Sub MakeSelectiongGetData()
Dim IE As New InternetExplorer
Const URL = "http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/"
Const optionText As String = "RDVT11"
Application.ScreenUpdating = False
With IE
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
Dim a As Object
Set a = .document.getElementById("ctl00_ddlAti")
Dim currentOption As Object
For Each currentOption In a.getElementsByTagName("Option")
If InStr(currentOption.innerText, optionText) > 0 Then
currentOption.Selected = True
Exit For
End If
Next currentOption
.document.getElementById("ctl00_x_agenda_r").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Dim nTable As HTMLTable
Do: On Error Resume Next: Set nTable = .document.getElementById("aGENDA"): On Error GoTo 0: DoEvents: Loop While nTable Is Nothing
Dim nRow As Object, nCell As Object, r As Long, c As Long
With ActiveSheet
Dim nBody As Object
Set nBody = nTable.getElementsByTagName("tbody")(0).getElementsByTagName("tr")
.Cells(1, 1) = nBody(0).innerText
For r = 2 To nBody.Length - 1
Set nRow = nBody(r)
For Each nCell In nRow.Cells
c = c + 1: .Cells(r + 1, c) = nCell.innerText
Next nCell
c = 0
Next r
End With
.Quit
End With
Application.ScreenUpdating = True
End Sub
Data on page (sample)
Code output (sample):
What I have to do is use Excel VBA to:
login to Amazon Seller
open a workbook
loop through a column to get an order number
put it in the search box
hit the search button
go to the order page and extract the data
then have the extracted data go back into a specified column in
another Excel workbook
The loop and order number parts are what I'm currently stumped on. I've figured out this much code as of this moment:
Sub MyAmazonSeller()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim oSignInLink As HTMLLinkElement
Dim oInputEmail As HTMLInputElement
Dim oInputPassword As HTMLInputElement
Dim oInputSigninButton As HTMLInputButtonElement
'InputSearchOrder will be the destination for order numbers taken from the workbook
Dim InputSearchOrder As HTMLInputElement
Dim InputSearchButton As HTMLInputButtonElement
Dim IE As InternetExplorer
Dim AAOrder As Workbook
Dim AAws As Worksheet
MyURL = "https://sellercentral.amazon.com/gp/homepage.html"
Set IE = New InternetExplorer
' Open the browser and navigate.
With IE
.Silent = True
.Navigate MyURL
.Visible = True
Do
DoEvents
Loop Until .ReadyState = READYSTATE_COMPLETE
End With
' Get the html document.
Set HTMLDoc = IE.Document
' See if you have the sign in link is because you are in the main
' page
Set oSignInLink = HTMLDoc.getElementById("signin-button-container")
If Not oSignInLink Is Nothing Then
oSignInLink.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
End If
' Get the email field and the next button
Set oInputEmail = HTMLDoc.getElementById("username")
Set oInputPassword = HTMLDoc.getElementById("password")
' Click the button and wait
oInputEmail.Value = "xxxxxx#xxxxxx.net"
' Get the password field and the sign in button
Set oInputPassword = HTMLDoc.getElementById("password")
Set oInputSigninButton = HTMLDoc.getElementById("sign-in-button")
' Click the button and wait
oInputPassword.Value = "xxxxxxxx"
oInputSigninButton.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Application.Wait (Now + TimeValue("0:00:05"))
Set AAOrder = Application.Workbooks.Open("Z:\Employee Folders\Employee\trackingnumber_sample_spreadsheet.xls")
Set AAws = AAws.Worksheets("PrimeOrdersWithNoFulfillmentRe")
Set InputSearchOrder = HTMLDoc.getElementById("sc-search-field")
'What I'm currently stuck on
InputSearchOrder.Value = "001-7163923-7572632"
Set InputSearchButton = HTMLDoc.getElementsByClassName("sc-search-button")(0)
InputSearchButton.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
'Was able to add this snippet, but I'm getting an error 13, most likely with
'my e variable. I'm basically trying to do a loop within a loop, extracting 5
'pieces of data and sticking them back into their respective columns in the
'original Excel sheet. The problem comes when scraping the HTML. I'm basically
'trying to get text in the tables which have a few levels and it's frustrating
'me to no end.
With HTMLDoc
Set elems = HTMLDoc.getElementsByTagName("td")
For Each e In elems
If e.innerText Like "*1Z*" Then
Range("D2").Value = e.innerText
End If
Next e
End With
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub