I am scraping a website with credentials and I have to click an element (button) that exports a pdf file (not as save as window).
The button directly exports the pdf
You specify the default download at the start with SetPreference
. You could then rename the file by finding the latest modified file in that folder (in this case I use file system object with .csv mask)
If you have an actual URL for download then use URLMon or binary download.
Option Explicit
Public Sub SpecifyDownloadFolder()
Dim d As WebDriver, filename As String, myFolder As Object
Const URL = "https://www.stats.govt.nz/large-datasets/csv-files-for-download/"
Const DOWNLOAD_DIRECTORY As String = "C:\Users\User\Downloads"
Const FILE_NAME As String = "myNewCsv.csv"
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = New ChromeDriver
With d
.SetPreference "download.default_directory", DOWNLOAD_DIRECTORY
.SetPreference "download.directory_upgrade", True 'safeguard
.SetPreference "download.prompt_for_download", False 'safeguard
.get URL
.FindElementByCss("h3 [download]").Click
Application.Wait Now + TimeSerial(0, 0, 5)
d.Quit
End With
Set myFolder = fso.GetFolder(DOWNLOAD_DIRECTORY)
Dim objFile As Object, dteFile As Date
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If objFile.DateLastModified > dteFile And fso.GetExtensionName(objFile.Path) = "csv" Then
dteFile = objFile.DateLastModified
filename = objFile.NAME
End If
Next objFile
If filename <> vbNullString And Not fso.FileExists(DOWNLOAD_DIRECTORY & "\" & FILE_NAME) Then
fso.MoveFile DOWNLOAD_DIRECTORY & "\" & filename, DOWNLOAD_DIRECTORY & "\" & FILE_NAME
End If
End Sub