Это, как я автоматизирована это в VBA
:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private Declare Function SetCursorPos Lib "user32" _
(ByVal X As Integer, ByVal Y As Integer) As Long
Private Declare Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Long, _
ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal _
hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As _
Integer, ByVal cy As Integer, ByVal wFlags As Integer)
'~~> Constants for pressing left button of the mouse
Private Const MOUSEEVENTF_LEFTDOWN As Long = &H2
'~~> Constants for Releasing left button of the mouse
Private Const MOUSEEVENTF_LEFTUP As Long = &H4
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Dim ie As InternetExplorer
Sub GetFiles()
Set ie = New InternetExplorer
GetFileFromUrl "http://daccess-ods.un.org/access.nsf/Get?Open&DS=A/HRC/WGAD/2015/28&Lang=E"
GetFileFromUrl "http://daccess-ods.un.org/access.nsf/Get?Open&DS=A/HRC/WGAD/2015/31&Lang=F"
End Sub
Sub GetFileFromUrl(url As String)
Dim pos As RECT
ie.Navigate url
ie.Visible = True
While ie.ReadyState <> 4
DoEvents
Wend
Sleep 7000
ie.ExecWB 4, 1, "c:\test.pdf"
Sleep 5000
SaveAsHwnd = FindWindow(vbNullString, "Save As")
If SaveAsHwnd <> 0 Then
Debug.Print "Found Save As window"
Else
Debug.Print "Did not find Save As window"
End If
SaveButtonHwnd = FindWindowEx(SaveAsHwnd, ByVal 0&, "Button", "&Save")
If SaveButtonHwnd <> 0 Then
Debug.Print "Found Save button"
' click button
'res = SendMessage(SaveButtonHwnd, TCM_SETCURFOCUS, 1, ByVal 0&)
'res = PostMessage(SaveButtonHwnd, BM_CLICK, ByVal 0&, ByVal 0&)
'res = SendMessage(SaveButtonHwnd, WM_COMMAND, 0&, 0&)
GetWindowRect SaveButtonHwnd, pos
'~~> Move the cursor to the specified screen coordinates.
SetCursorPos (pos.Left - 10), (pos.Top - 10)
'~~> Suspends the execution of the current thread for a specified interval.
'~~> This give ample amount time for the API to position the cursor
Sleep 100
SetCursorPos pos.Left, pos.Top
Sleep 100
SetCursorPos (pos.Left + pos.Right)/2, (pos.Top + pos.Bottom)/2
'~~> Set the size, position, and Z order of "File Download" Window
SetWindowPos Ret, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
Sleep 100
'~~> Simulate mouse motion and click the button
'~~> Simulate LEFT CLICK
mouse_event MOUSEEVENTF_LEFTDOWN, (pos.Left + pos.Right)/2, (pos.Top + pos.Bottom)/2, 0, 0
Sleep 700
'~~> Simulate Release of LEFT CLICK
mouse_event MOUSEEVENTF_LEFTUP, (pos.Left + pos.Right)/2, (pos.Top + pos.Bottom)/2, 0, 0
Else
Debug.Print "Did not find Save button"
End If
Sleep 5000
End Sub
В качестве альтернативы, UIAutomation
COM
объект может быть использован:
Sub GetFilesAutomation()
Dim o As IUIAutomation
Dim e As IUIAutomationElement
Dim SaveAsHwnd As LongPtr
Dim ie As New InternetExplorer
Set o = New CUIAutomation
ie.Navigate "http://daccess-ods.un.org/access.nsf/Get?Open&DS=A/HRC/WGAD/2015/28&Lang=E"
ie.Visible = True
Sleep 10000
ie.ExecWB 4, 1
Sleep 5000
SaveAsHwnd = FindWindow(vbNullString, "Save As")
Set e = o.ElementFromHandle(ByVal SaveAsHwnd)
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
End Sub
Вы, очевидно, дали хороший ответ, который решает проблему. Но только одно: вы говорите, что 'mech-> get ($ link)' не будет работать без полного URL-адреса. Это неверно. Я могу получить относительные ссылки с 'get'. – CJ7
Извините. Я не знаю, почему я так думал. Я, должно быть, сделал еще одну ошибку. Я обновлю ответ. Вы можете использовать '-> follow_link' или' -> get' по своему усмотрению. Если вы используете '-> get', вы можете отправить объект WWW :: Mechanize :: Link. – bolav