问题描述
我正在用凭据刮擦一个网站,我必须单击一个导出pdf文件的元素(按钮)(而不是另存为窗口).
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).
该按钮直接将pdf文件导出到C:\Users\User\Downloads
.
The button directly exports the pdf file to C:\Users\User\Downloads
.
如何将目录更改为桌面?
How can I change the directory to the desktop instead?
还要重命名文件吗?
用于单击此按钮的代码
On Error Resume Next
Do While .FindElementByXPath("//*[@id='theBttnbobjid_1545645103945_dialog_submitBtn']") Is Nothing
DoEvents
Loop
On Error GoTo 0
.FindElementByXPath("//*[@id='theBttnbobjid_1545645103945_dialog_submitBtn']").Click
这是与该按钮相关的html
This is the html related to that button
<tbody><tr valign="middle"><td height="21" width="5" style="background-image:url('aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/../dhtmllib/images/skin_standard/button.gif');background-position:0px 0px;"></td><td id="theBttnCenterImgbobjid_1545648005071_dialog_submitBtn" align="center" class="wizbutton" style="padding-left:3px;padding-right:3px;background-image:url('aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/../dhtmllib/images/skin_standard/button.gif');background-position:0px -42px;"><nobr><a id="theBttnbobjid_1545648005071_dialog_submitBtn" href="javascript:void(0)" class="wizbutton" role="button">Export</a></nobr></td><td height="21" width="5" style="background-image:url('aspnet_client/system_web/4_0_30319/crystalreportviewers13/js/crviewer/../dhtmllib/images/skin_standard/button.gif');background-position:0px -21px;"></td></tr></tbody>
推荐答案
您可以从SetPreference
开始指定默认下载.然后,您可以通过在该文件夹中找到最新的修改文件来重命名该文件(在这种情况下,我使用带.csv掩码的文件系统对象)
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)
如果您有实际的URL下载,请使用 URLMon或二进制下载.
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
这篇关于使用硒将文件下载到特定目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!