0

I'm trying to extract price from a website by using ie.doc.innertext and asterisk but its not working.

For example, the price in innertext is: "Cart Total: $25"

My condition is: If the words "Item added to the cart" exist in innertext and "Cart Total" exist in innertext, then extract the part after colon in the innertext Cart Total: $25

It's $25 which I want.

My code is:

Sub GetPrice()

Dim IE As New InternetExplorer
Dim doc As HTMLDocument


IE.Visible = True
IE.Navigate " http://www.example.com"

Do Until IE.ReadyState = READYSTATE_COMPLETE
    DoEvents
Loop

Set doc = IE.Document
doc.forms("shipform").Elements("upcs").Value = "025192197925"

Application.Wait Now + TimeValue("00:00:01")


doc.forms("shipform").Elements("submit").Click

Do Until IE.ReadyState = READYSTATE_COMPLETE
    DoEvents
Loop

Application.Wait Now + TimeValue("00:00:01")

html_code = doc.body.innerText

If InStr(html_code, "Item added to the cart") > 0 Then

    If InStr(html_code, "Cart Total " & "*") > 0 Then
        the_string = Right("Cart Total " & "*", 15)
        MsgBox the_string
    End If

ElseIf InStr(html_code, "Item not added") > 0 Then
    MsgBox "Not added"
End If

Set IE = Nothing

End Sub

I'd highly appreciate your help. Thank you

1
  • 1
    Instr will treat "*" as a literal asterisk, not a wildcard, so remove that from your test. Commented Aug 8, 2017 at 16:26

2 Answers 2

1

Something like this should work:

Dim pos as Long

If InStr(html_code, "Item added to the cart") > 0 Then

    pos = InStr(html_code, "Cart Total")

    If pos > 0 Then

        the_string = Mid(html_code,pos+10, 5)
        MsgBox the_string

    End If

Else Then
    MsgBox "Not added"
End If
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Tim, thank you for the code. It works flawlessly. I also searched how MID function works but couldn't get the point in the light of my question. So what Mid(html_code, pos+10, 5) is doing? Is it adding 10 more characters to Cart Total and then from Cart Total + 10, its giving us 5 from the right ? Thank you for your help.
Instr gives you the position of where "Cart total" starts, so the +10 moves past that to begin capturing after "Cart total".
Thank you for your help, explanation and time Tim. I highly appreciate it. :)
0

try these to give you an idea

debug.print split("Cart Total: $25")(2)          ' space is the default split character

or

debug.print split("Cart Total: $25", ":")(1)  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.