Input data into web page then extracting nested span field into Excel using VBA

JP12345

New Member
Joined
Sep 11, 2018
Messages
9
Hi - Any help on the below issues would be greatly appreciated.

Using VBA I am trying to input a field (serial number e.g. PC0X5YHZ) into a web page and then extract data...
https://pcsupport.lenovo.com/us/en/...nty and Repair:Warranty|Check Warranty Status

Problem one: I can insert the serial into the text field but can't seem to find a way to code for pressing the Check Warranty button (can't get 'click' or sending enter to work) and when I do manage to insert and manually press, the field just blanks and nothing is found.
Problem two: Once in the page I am trying to extract 3 fields:

Machine Type Model: = 20KES5SD09
Expiration Date: = 2021-08-23
Start Date: = 2018-08-24 (need to press the down arrow tag on right side to drop down further field info which contains this item)

Have managed the first two but can't seem to extract the date from the for Start Date.

My code so far (I have named my ranges and enabled the relevant references):

Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Row = Range("Serial_Number").Row And Target.Column = Range("Serial_Number").Column Then


Dim IE As New InternetExplorer
'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" ' I am using this hard coded line as can't manage to code pressing check warranty button - this bypasses that need but no good for multiple serials


Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.document


Dim Class As String
Class = Trim(Doc.getElementsByClassName("mt-code")(0).innerText)
Range("Model_Number").Value = Class


Dim Expiration As Date
Expriation = Trim(Doc.getElementsByClassName("expirationDate")(0).innerText)
Range("End_Date").Value = Expriation


Dim Start As Date
Start = Trim(Doc.getElementsByClassName("daysTips")(0)(span,0)(2).innerText) 'The bold text errors. There are multiple text elements to the span & I don't know how to extract.
Range("Start_Date").Value = Start
IE.Quit
End If


End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
problem 2 solved: any help one the 1st one?

Sub GetWarrantyInfo()

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

Sheet1.Range("B6") = Trim(Doc.getElementsByClassName("mt-code")(0).innerText)
Sheet1.Range("B5") = Trim(Doc.getElementsByClassName("expirationDate")(0).innerText)


Doc.querySelector(".icon.icon-s-down").Click

Sheet1.Range("B4") = IE.document.querySelector(".daysTips span").innerText

Sheet1.Range("B4") = Right(Range("B4"), Len(Range("B4")) - 12)

IE.Quit

End Sub
 
Upvote 0
problem 2 solved: any help one the 1st one?

Try this:

Code:
Option Explicit

Sub PushYourButton()

    Dim IE As New SHDocVw.InternetExplorer
    Dim htmlDoc As MSHTML.HTMLDocument
    Dim HTMLInput As MSHTML.IHTMLElement
    Dim HTMLButtons As MSHTML.IHTMLElementCollection
    Dim HTMLButton As MSHTML.IHTMLElement
    
    IE.Visible = True
    IE.navigate "https://pcsupport.lenovo.com/nl/nl/warrantylookup?linkTrack=Mast:SubNav:Support:Warranty%20and%20Repair:Warranty%7CCheck%20Warranty%20Status"
    
    Do While IE.ReadyState <> READYSTATE_COMPLETE
    Loop
    
    Set htmlDoc = IE.Document
    
    'Look for the input textbox with id "input_sn"
    Set HTMLInput = htmlDoc.getElementById("input_sn")
    'Enter the lookup code
    HTMLInput.Value = "PC0X5YHZ"
    
    'Address all buttons
    Set HTMLButtons = htmlDoc.getElementsByTagName("button")
    
    'Loop through all buttons
    For Each HTMLButton In HTMLButtons
    'Find the submit button with classname "btn btn-primary" and click it
        If HTMLButton.className = "btn btn-primary" Then
            HTMLButton.Click
            Exit For
        End If
    Next HTMLButton
End Sub
 
Last edited:
Upvote 0
Thank you, but unfortunately gives me the same issue, think the problem lies with the input field itself, even though I can see my input in the screen not sure its the right field. I also tried to active the field 1st before pasting but no luck there either.
 
Last edited:
Upvote 0
found the issue.. someone suggested the input field required another trigger so added an application.wait command and manually tested by adding and removing a character from the end of the input field!.. just need to find a way to automate this keyboard event now.
 
Upvote 0
Issue resolved using the following
Code:
 [COLOR=#303336][FONT=inherit]Doc[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]getElementsByName[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"input_sn"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Item[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"input_sn"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Value [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"PC0X5YHZ"[/FONT][/COLOR][COLOR=#303336][FONT=inherit][/FONT][/COLOR]<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">[COLOR=#303336][FONT=inherit]Doc[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]getElementById[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"input_sn"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Focus
SendKeys [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"z+{BS}"[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]True[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR]</code>[COLOR=#303336][FONT=inherit]Doc[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]querySelector[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]".btn.btn-primary"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Click[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top