selenium +vba table

cgpa

New Member
Joined
Jan 23, 2023
Messages
11
Office Version
  1. 2007
Platform
  1. Windows
Hello guys.
I need to download a table daily, the code even works, but it copies the table in a disorganized way. I would like it to copy the data in order, as on the website.

Sub ExtrairTabelaDaPagina()

Dim driver As New ChromeDriver



With driver
.Start
.Get "https://www2.bmf.com.br/pages/porta...regao-ptBR.asp?Data=20/02/2020&Mercadoria=IND"
.Window.Maximize

.FindElementById("principal").AsTable.ToExcel Plan1.range("a1")

End With

End Sub
 

Attachments

  • table.png
    table.png
    161.2 KB · Views: 56

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The main table contains 3 nested tables which you can extract separately:
VBA Code:
Public Sub Chrome_Extract_Table()
    
    Dim driver As Selenium.ChromeDriver
    Dim table As WebElement
    Dim destSheet As Worksheet
    
    Set destSheet = ActiveSheet
    
    Set driver = New Selenium.ChromeDriver
    With driver
        .Start
        .Get "https://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-sistema-pregao-ptBR.asp?Data=20/02/2020&Mercadoria=IND"
    End With
    
    With destSheet
        .Cells.Clear
        .Range("A1").Value = "Dados"
        .Range("B1").Value = "Volume"
        .Range("B1:F1").HorizontalAlignment = xlCenter
        .Range("B1:F1").Merge
        .Range("G1").Value = "Dados"
        .Range("G1:P1").HorizontalAlignment = xlCenter
        .Range("G1:P1").Merge
    End With
    
    Set table = driver.FindElementById("MercadoFut0").FindElementByTag("table")
    table.AsTable.ToExcel destSheet.Range("A2")

    Set table = driver.FindElementById("MercadoFut1").FindElementByTag("table")
    table.AsTable.ToExcel destSheet.Range("B2")
    
    Set table = driver.FindElementById("MercadoFut2").FindElementByTag("table")
    table.AsTable.ToExcel destSheet.Range("G2")
    
End Sub
 
Upvote 1
Solution
It is perfect!
Would it be possible to copy the date too? I attached an image for you to see better.
 

Attachments

  • data.png
    data.png
    47.9 KB · Views: 24
Upvote 0
I tried to save in cell "Q5", but nothing returns
 
Upvote 0
This works for me:
VBA Code:
    destSheet.Range("Q5").Value = driver.FindElementById("dData1").Value
 
Upvote 0
worked perfectly! Thank you very much

let me clear a doubt, in selenium is it possible to download a file, for example pdf or in zip format and download it directly to a specific folder?

for example the code below I can click and download the file, but I don't know how to put it in a specific folder, I wanted to put it in local path = "C:\Users\pc\Desktop\PD\PD.XLS"

Sub ExtrairTabelaDaPagina()

Dim driver As New ChromeDriver


With driver
.Start
.Get "BDI"






.FindElementByXPath("/html/body/div/div/div[1]/div/div/div[3]/div/div/div[1]/div[2]/div/div[2]/button").Click
Application.Wait Now + TimeValue("00:00:03")

.FindElementByXPath("/html/body/div/div/div[1]/div/div/div[3]/div/div/div[1]/div[2]/div/div[2]/div/a[2]/span[2]").Click
Application.Wait Now + TimeValue("00:00:02")




End With

End Sub
 
Upvote 0
let me clear a doubt, in selenium is it possible to download a file, for example pdf or in zip format and download it directly to a specific folder?
Sorry, I don't know if that's possible.

You could do it with the Windows API function URLDownloadToFile if you have a direct URL to the file.
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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