https://www.laquintaca.gov/connect/short-term-vacation-rentals
上面的站點有一組影像。在這些影像上是一個帶有文本值“ACTIVE AND SUSPENDED PERMITS”的框。單擊影像會將您帶到可下載的 PDF。我想將該下載程序自動化到我的桌面。當他們上傳新的 PDF 時,“橙色”影像保持不變,但 href 地址每兩周更改一次(檔案序列號在末尾)。
更直接地說,我如何單擊 orage 影像空間以訪問底層但每兩周更改一次的 PDF?
我找不到可操作的物件地址以單擊以將我帶到可下載的檔案。什么是“單擊”的 VBA Selenium 代碼行
'****************************************************** ******************************
Sub FindingElements()
Set cd = New Selenium.ChromeDriver
cd.Start
cd.Get "https://www.laquintaca.gov/connect/short-term-vacation-rentals"
'"https://www.laquintaca.gov/connect/short-term-vacation-rentals"
'=================================================================
'Find Element By ID or Name
'=================================================================
Dim SearchInput As Selenium.WebElement
Dim SearchButton As Selenium.WebElement
Dim FindBy As New Selenium.By
'
If Not cd.IsElementPresent(FindBy.ID("OuterContainer")) Then
MsgBox "Could not find search input box"
Exit Sub
End If
'
' Set SearchInput = cd.FindElementById("OuterContainer")
' Set SearchInput = cd.FindElement(FindBy.ID("searchInput"))
' Set SearchInput = cd.FindElementByName("search")
' Set SearchInput = cd.FindElementByCss("#searchInput")
' Set SearchInput = cd.FindElementByCss("[name='search']")
' Set SearchInput = cd.FindElementByXPath("//*[@id='searchInput']")
uj5u.com熱心網友回復:
您可以使用 CSS 或 XPath 來完成。我更喜歡 CSS,因為 XPath 更復雜,有時比 CSS 更慢。
第一個程序一直等到檔案下載完畢。
我使用Paul_Hossler的程式來查找ChromeDownloadFolder。
Sub DownloadFileFromLaquintaca()
Dim cd As New Selenium.ChromeDriver
Dim DefaultChromeDownloadFolder As String
' Get Chrome download folder
DefaultChromeDownloadFolder = ChromeDownloadFolder
' Start Chrome
cd.Start
' Navigate to
cd.Get "https://www.laquintaca.gov/connect/short-term-vacation-rentals"
Dim FindBy As New Selenium.By
Dim imgElement As Selenium.WebElement
' Check if element is present with CSS
If Not cd.IsElementPresent(FindBy.Css("img[alt='ACTIVE & SUSPENDED PERMITS BOX']")) Then
MsgBox "Could not find image box"
Exit Sub
End If
' Click to download
cd.FindElementByCss("img[alt='ACTIVE & SUSPENDED PERMITS BOX']").Click
' Wait until download is completed
Do While Dir(DefaultChromeDownloadFolder & "\" & "STVRCurrentActiveSuspended.pdf") = ""
DoEvents
Loop
End Sub
Function ChromeDownloadFolder()
' By Paul_Hossler
Dim sPref As String
Dim iFile As Long, iStart As Long, iEnd As Long
Dim sBuffer As String, sSearch As String, sDownloads As String
' Chrome preferces file, no extension
sPref = Environ("LOCALAPPDATA") & "\Google\Chrome\User Data\Default\Preferences"
' marker
sSearch = """download"":{""default_directory"":"
' read the whole file into buffer
iFile = FreeFile
Open sPref For Input As #iFile
sBuffer = Input$(LOF(iFile), iFile)
Close #iFile
' find start of marker
iStart = InStr(1, sBuffer, sSearch, vbTextCompare)
' find comma
iEnd = InStr(iStart Len(sSearch), sBuffer, ",", vbTextCompare)
' pull out path
sDownloads = Mid(sBuffer, iStart Len(sSearch) 1, iEnd - iStart - Len(sSearch) - 2)
' remove double back slashes
ChromeDownloadFolder = Replace(sDownloads, "\\", "\")
End Function
uj5u.com熱心網友回復:
我解決了我在下面的 SUB 中遇到的問題,效果很好。非常感謝您的出色幫助。代碼切換到當前代碼選項卡,將 URL 變為紅色,然后通過 WinHttpReq = CreateObject("Microsoft.XMLHTTP") 行程下載到某個位置:
Sub DownloadFileFromLaquintaca2()
Dim cd As New selenium.ChromeDriver
Dim DefaultChromeDownloadFolder As String
Dim MyURL As String
' Start Chrome
Set cd = New ChromeDriver
cd.Start
' Navigate to
cd.get "https://www.laquintaca.gov/connect/short-term-vacation-rentals"
Const URL = "https://www.laquintaca.gov/connect/short-term-vacation-rentals"
Dim FindBy As New selenium.By
Dim imgElement As selenium.WebElement
' Check if element is present with CSS
If Not cd.IsElementPresent(FindBy.Css("img[alt='ACTIVE & SUSPENDED PERMITS BOX']")) Then
MsgBox "Could not find image box"
Exit Sub
End If
cd.FindElementByCss("img[alt='ACTIVE & SUSPENDED PERMITS BOX']").Click
Application.Wait (Now TimeValue("0:00:3"))
'Get URL Address in Second Chorme Tab
With cd
'.get URL
.SwitchToNextWindow
MyURL = .URL
'Debug.Print .Window.Title
'Debug.Print myURL
.Windows.Item(.Windows.Count - 1).Close 'close prior window
End With
'Write PDF File into Location with file Name STVR.PDF ---D:\MyDownLoads\STVR.pdf
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", MyURL, False, "username", "password"
WinHttpReq.send
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile "D:\MyDownLoads\STVR.pdf", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/406288.html
標籤:
上一篇:根據資料將列格式從垂直更改為水平
