Copy value from a webpage into a variable

reniantony

New Member
Joined
May 24, 2017
Messages
8
Hello Excel Experts! Need your help on a simple automation program.

There is a website where it has some data and I need to put it into a variable and get it displayed. In more detail, my program accesses the NSE website to the stock name, and I need the % DQTQ value to be fetched into the variable and displayed on the screen.

The code seems to run well, but the problem is that the DQTQ value is never captured, its always blank. I've mentioned the code and the snapshot of the ID below.

The code is as follows:

=============================
Sub dqtq()

Dim driver As New WebDriver
Dim stockname As String
Dim dqtq As String
Dim dqtq1 As String

stockname = InputBox("Please enter Stockname", "Input stockname")
driver.Wait (500)
driver.Start "chrome"
'driver.Window.Minimize

driver.Wait (1000)
driver.Get "https://www.nseindia.com/get-quotes/equity?symbol=" & stockname

driver.Wait (1000)

dqtq = driver.FindElementById("securityWiseDQTQ").Value

driver.FindElementById("securityWiseDQTQ").ScrollIntoView

MsgBox "DQTQ is " & dqtq

End Sub
=============================

Capture.PNG
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,

Use below code.

'This program requires references to the following in Tools -> References: (see image)
'1 Microsoft Internet Controls
'2. Microsoft HTML Object Library

VBA Code:
Sub getStockData()
    On Error Resume Next
    Application.ScreenUpdating = False
    Dim HTMLDoc As New HTMLDocument
    Dim ieBrowser As New InternetExplorer
    Dim qtyTraded As String, dQ As String, DQTQ As String
    Dim stock As String
    
    stock = InputBox("Enter Stock Code")
    
    'To open and show Internet Explorer
    'ieBrowser.Visible = True
    
    'To Open website in Internet Explorer
    ieBrowser.navigate "https://www.nseindia.com/get-quotes/equity?symbol=" & stock
    
    Do
    ' Wait till the Browser is loaded
    Loop Until ieBrowser.readyState = READYSTATE_COMPLETE
    
    Set HTMLDoc = ieBrowser.document
    
    Application.Wait (Now + TimeValue("00:00:03"))
      
    qtyTraded = HTMLDoc.getElementById("securityWiseQT").innerHTML
    dQ = HTMLDoc.getElementById("securityWiseDQ").innerText
    DQTQ = HTMLDoc.getElementById("securityWiseDQTQ").innerText

    ActiveSheet.Range("A2") = stock
    ActiveSheet.Range("B2") = qtyTraded
    ActiveSheet.Range("C2") = dQ
    ActiveSheet.Range("D2") = DQTQ
    
    MsgBox "Data Copied"
    Set ieBrowser = Nothing
    Set HTMLDoc = Nothing
    
    Application.ScreenUpdating = True
End Sub
 

Attachments

  • Referencesforwebscraping.PNG
    Referencesforwebscraping.PNG
    21.1 KB · Views: 4
Upvote 0
Solution

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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