scrape info from nested div excel vba - html

I'm trying to extract the start date from the following, but my code cant seem to see the 'nested" div "daysTips" any help would be great. Thank you.
HTML Screen Shot:
Sub warranty2()
Dim elements As IHTMLElementCollection
Dim IE As New InternetExplorer
Dim Doc As HTMLDocument
IE.Visible = True
IE.navigate "https://pcsupport.lenovo.com/gb/en/products/laptops-and-netbooks/thinkpad-x-series-laptops/thinkpad-x280-type-20kf-20ke/20ke/20kes5sd09/pc0x5yhz/warranty"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set Doc = IE.document
Set elements = Doc.getElementsByClassName("daysTips")
For i = 0 To elements.Length - 1
Sheet1.Range("G" & (i + 1)) = elements(i).innerText
Next i
IE.Quit
End Sub

You need to do the click first on the down chevron
HTML:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer
With IE
.Visible = True
.navigate "https://pcsupport.lenovo.com/gb/en/products/laptops-and-netbooks/thinkpad-x-series-laptops/thinkpad-x280-type-20kf-20ke/20ke/20kes5sd09/pc0x5yhz/warranty"
While .Busy Or .readyState < 4: DoEvents: Wend
IE.document.querySelector(".icon.icon-s-down").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Debug.Print IE.document.querySelector(".daysTips span").innerText
.Quit
End With
End Sub

Related

Click on Table Link

I am completely new to excel vba and i want to do it for a specific requirement how to click on the Unit history button in the website. Could anyone help me with a program?
I used the following working template code:
Sub ClickLink()
Dim htmlDoc As MSHTML.HTMLDocument
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Set objIE = New SHDocVw.InternetExplorer
With objIE
.navigate "https://www.google.com"
.Visible = 1
Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop
Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("A")
For Each htmlInput In htmlColl
If htmlInput.innerText = "Gmail" Then
htmlInput.Focus
htmlInput.Click
Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop
Exit For
End If
'Debug.Print "Frame " & x & ": " & htmlInput.innerText
Next htmlInput
Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("A")
For Each htmlInput In htmlColl
If htmlInput.innerText = "Sign in" Then
htmlInput.Focus htmlInput.Click
Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop
Exit For
End If
'Debug.Print "Frame " & x & ": " & htmlInput.innerText
Next htmlInput
End With
End Sub
Replaced url with my url such that I had:
Sub ClickLink()
Dim htmlDoc As MSHTML.HTMLDocument
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Set objIE = New SHDocVw.InternetExplorer
With objIE
.navigate "http://insraa01.asia.delphiauto.net/"
.Visible = 1
Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop
Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("A")
For Each htmlInput In htmlColl
If htmlInput.innerText = "Unit History" Then
htmlInput.Focus
htmlInput.Click
Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop
Exit For
End If
'Debug.Print "Frame " & x & ": " & htmlInput.innerText
Next htmlInput
End With
End Sub
It is not working because my desired element to click is a table row and I don't know how to do this. I have gone through Clicking Table Row in HTML Table using VBA but remain confused as to taking reference to the tables and clicking on Table contents.
The element to click on is highlighted in the image.
You can try a css class selector in combination with href attribute selector and ends with operator to target the a tag of interest (the fact it is within a table is irrelevant) and it looks like you have a parent frame to negotiate as well.
ie.document.querySelector("frame").item(index of frame).contentDocument.querySelector(".nonvis[href$=unithistory]").click
This says to click the first element which has class nonvis and href attribute who's value ends with unithistory.
You need to determine at which index of frames the frame is that contains your a tag e.g. the first frame would be 0, the second would be 1 etc.
Read about css selectors here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

Excel VBA to activate selectitem in web and go web for data

I'm very fresh to excel VBA. I tried use all potential solutions in the internet for activate selectitem in web and get data table but it didn't work. My Excel VBA code and Javascript is below.
Sub GetQuarterFinancials()
Dim ie As Object, URL As String
URL = "https://www.marketwatch.com/investing/stock/aapl/financials"
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate URL
Do While .Busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
Set ticker = ie.document.getElementById("autocomplete_input")
ticker.value = "FB"
.document.getElementById("investing_ac_button").Click
Application.Wait (Now + TimeValue("00:00:05"))
Set selectitem = ie.document.getElementsByTagName("select")
For Each i In selectitem
i.value = "/investing/stock/msci/financials/income/quarter"
i.FireEvent ("onchange")
Next i
End With
End Sub
<select style="float:right" onchange="window.location = this.options[selectedIndex].value;">
<option value="/investing/stock/msci/financials" selected="selected">Annual Financials</option>
<option value="/investing/stock/msci/financials/income/quarter" >Quarter Financials</option>
</select>
<div style="clear:both"></div>
</div>
<div class="block">
<h2>Annual Financials for MSCI Inc.</h2>
You can use a css attribute = value selector to target the appropriate option from dropdown then select it.
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub MakeSelections()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/aapl/financials"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#autocomplete_input").Value = "FB" 'you could avoid search by adding ticker into url
.querySelector("#investing_ac_button").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Dim ele As Object
With .document
Do
On Error GoTo 0
Set ele = .querySelector("[value^='/investing/stock/fb/financials/income/quarter']")
On Error Resume Next
Loop While ele Is Nothing
.querySelector("[value^='/investing/stock/fb/financials/income/quarter']").Selected = True
.querySelector(".financials select").FireEvent "onchange"
Stop 'delete me later
End With
.Quit
End With
End Sub

Second drop down in HTML is not getting selected through VBA

My VBA code is not able to select the second drop down option once the first drop down is selected. Not sure why one dropdown is loading and second is not responding as per below code? Appreciate if you could help on fixing this. Regards
Dim IE As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim commodityStr As String
Dim commodityObj As HTMLObjectElement
Dim commodityCodes As IHTMLElementCollection
Dim codeCounter As Long
Dim EDateStr As String
Dim EDateObj As HTMLObjectElement
Dim EdateCodes As IHTMLElementCollection
Dim i As Integer
commodityStr = "MADHYA PRADESH"
EDateStr = "REWA"
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate "http://hydro.imd.gov.in/hydrometweb/(S(ryta1dvaec5pg03bdnxa5545))/DistrictRaifall.aspx"
While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend
Set HTMLDoc = IE.document
End With
Set commodityObj = HTMLDoc.getElementById("listItems")
For codeCounter = 0 To commodityObj.Length - 1
If commodityObj(codeCounter).innerText = commodityStr Then
commodityObj.Value = commodityObj(codeCounter).Value
commodityObj.Focus
commodityObj.FireEvent ("onchange")
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: Wend
Exit For
End If
Next
Set EDateObj = HTMLDoc.getElementById("DistrictDropDownList")
For codeCounter = 0 To EDateObj.Length - 1
If EDateObj(codeCounter).innerText = EDateStr Then
EDateObj.Value = EDateObj(codeCounter).Value
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: Wend
commodityObj.Focus
commodityObj.FireEvent ("onchange")
Exit For
End If
Next
You need a timed loop to allow for that execution of the onchange event. Also, you can make use of css attribute = value selectors to remove loops when targeting elements. It is likely the page does an XHR request so inspect via dev tools to see if that is the case.
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub MakeSelections()
Dim ie As InternetExplorer, ele As Object, t As Date
Const MAX_WAIT_SEC As Long = 5
Dim commodity As String, iDate As String
commodity = "MADHYA PRADESH"
iDate = "REWA"
Set ie = New InternetExplorer
With ie
.Visible = True
.Navigate2 "http://hydro.imd.gov.in/hydrometweb/(S(3qitcijd521egpzhwqq3jk55))/DistrictRaifall.aspx"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("[value='" & commodity & "']").Selected = True
.document.querySelector("[name=listItems]").FireEvent "onchange"
t = Timer
Do
On Error Resume Next
Set ele = .document.querySelector("[value='" & iDate & "']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
ele.Selected = True
.document.querySelector("#GoBtn").Click
Else
Exit Sub
End If
Stop
.Quit
End With
End Sub

VBA Web Automation on a dynamic site

I am trying to scrap barcode owners from this site : http://gepir.gs1.org/index.php/search-by-gtin
I am receiving "Object doesnt support this property or method" error msg because of the final line. I tried to extract it with class and tag but failed.
I am trying to extract the company name from the dynamic table.
Sub aa()
Dim ie As New SHDocVw.InternetExplorer
Dim doc As HTMLDocument
Dim aaa As Object
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
ie.Visible = True
ie.navigate "gepir.gs1.org/index.php/search-by-gtin"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Debug.Print ie.LocationName, ie.LocationURL
Set doc = ie.document
ie.document.forms("searchbygtin").elements("keyValue").Value = "685387379712"
ie.document.forms("searchbygtin").elements("method").Click
Debug.Print ie.document.getElementsBytag("tr")(7).innerText
End Sub
Any help would be great,
Thanks
You need TagName
Debug.Print ie.document.getElementsByTagName("tr")(7).innerText
I get a captcha though so I doubt you can complete this task in this way. Selenium and IE both seem to trigger the captcha, at least for me.
If not for the captcha I would re-write your script as follows (perhaps with a loop to make sure tr element is present):
Option Explicit
Public Sub GetLastChangeDate()
Dim ie As New SHDocVw.InternetExplorer, doc As HTMLDocument
Const URL = "gepir.gs1.org/index.php/search-by-gtin"
With ie
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
Set doc = .document
Debug.Print .LocationName, .LocationURL
doc.getElementById("keyValue").Value = "685387379712"
doc.getElementById("submit-button").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Debug.Print .document.getElementsByTagName("tr")(7).innerText
End With
End Sub

Vba getElementById cause Run-time error '424'

Really appreciate if anyone can help.
The part of code always run into Run - time Error:
The HTML part is in the picture:
Thanks for attention.
Sub HTML_Table_To_Excel()
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.application")
With objIE
.Visible = True
.navigate ("http://www.global-rates.com/interest-rates/libor/libor.aspx")
End With
While objIE.Busy
Wend
Dim HTMLDoc As Object
Set HTMLDoc = objIE.document
Dim EuroButton As Object
Set EuroButton = HTMLDoc.getElementById("btn_eur")
objIE.Quit
Set objIE = Nothing
......
End Sub
It's not an element ID - it's a tag name; Set EuroButton = HTMLDoc.getElementsByTagName("btn_eur") then when you need to order it: EuroButton(0).[command here]
You were just fine with your selector. It does have that id. The problem is you need a proper wait for page load before attempting to click. Try the below. I have also updated to .navigate2 and removed the unnecessary ().
Option Explicit
Public Sub HtmlTableToExcel()
Dim objIE As Object, HTMLDoc As Object, euroButton As Object
Set objIE = CreateObject("InternetExplorer.application")
With objIE
.Visible = True
.Navigate2 "http://www.global-rates.com/interest-rates/libor/libor.aspx"
While .Busy Or .readyState < 4: DoEvents: Wend
Set HTMLDoc = .document
Set euroButton = HTMLDoc.getElementById("btn_eur")
euroButton.Click
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<==delete me later
'-----
'.Quit
End With
End Sub