Ganesh Balla
Ganesh Balla

Reputation: 1

Extracting table data from website using chrome

i want to extract table data from website with chrome browser with selenium. i wrote below code but it's not working

Sub Chartinka()
    Dim bot As New WebDriver, posts As WebElements, post As WebElement, i As Integer, mysheet As Worksheet, keys As Selenium.keys
    bot.Start "chrome", "https://chartink.com/screener/buy-15m-78"
    bot.Get "/"
    Set posts = bot.FindElementsByXPath("//*[@id='DataTables_Table_0']/tbody/tr[1]")
    i = 2
    Set mysheet = Sheets("Sheet3")
    For Each post In posts
            ' Run time Error '438' on the next line
            mysheet.Cells(i, 1).Value = post.FindElementByTag("td")(0).text
            mysheet.Cells(i, 2).Value = post.FindElementByTag("td")(1).text
            mysheet.Cells(i, 3).Value = post.FindElementByTag("td")(2).text
            mysheet.Cells(i, 4).Value = post.FindElementByTag("td")(3).text
            mysheet.Cells(i, 5).Value = post.FindElementByTag("td")(4).text
            mysheet.Cells(i, 6).Value = "BUY"
        i = i + 1
    Next
  bot.Quit
End Sub

Upvotes: 0

Views: 1628

Answers (2)

YasserKhalil
YasserKhalil

Reputation: 9568

@asmitu You can do without loop using such a code

Sub GetTable()
Const sURL As String = "https://chartink.com/screener/buy-15m-78"
Dim bot As New ChromeDriver, tbl As Selenium.TableElement

With bot
    .Get sURL

    Set tbl = .FindElementById("DataTables_Table_0").AsTable
    tbl.ToExcel ThisWorkbook.Sheets("Sheet1").Range("A1")
End With
End Sub

Upvotes: 1

asmitu
asmitu

Reputation: 73

The following script should fetch you the tabular content from that page.

Sub GetTabularcontent()
    Const Url$ = "https://chartink.com/screener/buy-15m-78"
    Dim driver As New ChromeDriver, tRow As Object
    Dim tCel As Object, R&, C&

    With driver
        .get Url

        For Each tRow In .FindElementsByCss("#DataTables_Table_0 tr", Timeout:=10000)
            For Each tCel In tRow.FindElementsByCss("th,td")
                C = C + 1: Cells(R + 1, C) = tCel.Text
            Next tCel
            C = 0: R = R + 1
        Next tRow
    End With
End Sub

Upvotes: 1

Related Questions