excel - SendKeys到selenium VBA中打开对话框中的文件名字段

标签 excel vba selenium

我正在尝试设计一个打开特定 URL 的代码,在特定点,我遇到了与该网站相关的 OPEN 对话框。就像那样 enter image description here

现在我遇到的问题是,我需要将“C:\Users\User\Desktop\Pics(423).jpg”之类的特定路径粘贴到文件名字段,然后单击“打开”按钮

而且我也被困在如何使用剪贴板复制内存中的路径.. 感谢高级帮助

最佳答案

如果您想避免使用 SendKeys 方法,您可以使用 UIAutomationClient library或 user32.dll 中的 SendMessage 函数。这些将允许您使用 Windows API 与对话窗口进行交互。

关于VBA 中的UIAutomationClient 库的在线资源并不多,但是this video (和 part 2 )应该足以让您入门。

编辑: 但是,如果您决定使用 SendMessage,下面是一个如何实现它的示例:

1) 将以下内容放在代码模块的顶部

Option Explicit

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As String) As LongPtr
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr

Private Const BM_CLICK = &HF5
Private Const WM_SETTEXT = &HC

2) 添加以下子过程并将FileName的值更改为所需的值

Sub FillFileNameAndPressOpenButton()

    Dim FileName As Variant
    FileName = "(423)" 'Change this to the appropriate file name

    Dim WindowCaption As String
    WindowCaption = "Open"

    Dim OpenDialogHandle As LongPtr
    OpenDialogHandle = FindWindow(vbNullString, WindowCaption)

    If OpenDialogHandle = 0 Then
        MsgBox "Couldn't find the Window"
        Exit Sub
    End If

    Dim TextBoxHandle As LongPtr, tempHandle As LongPtr
    tempHandle = FindWindowEx(OpenDialogHandle, 0, "ComboBoxEx32", vbNullString)
    tempHandle = FindWindowEx(tempHandle, 0, "ComboBox", vbNullString)
    TextBoxHandle = FindWindowEx(tempHandle, 0, "Edit", vbNullString)

    If TextBoxHandle = 0 Then
        MsgBox "Couldn't find the textbox"
        Exit Sub
    End If

    Dim Rslt As LongPtr
    Rslt = SendMessage(TextBoxHandle, WM_SETTEXT, 0, FileName)
    DoEvents

    Dim ButtonHandle As LongPtr, ButtonCaption As String
    ButtonCaption = "&Open"
    ButtonHandle = FindWindowEx(OpenDialogHandle, 0, "Button", ButtonCaption)

    If ButtonHandle = 0 Then
        MsgBox " Couldn't find the button"
        Exit Sub
    End If

    Rslt = SendMessage(ButtonHandle, BM_CLICK, 0, 0)
    DoEvents

End Sub

关于excel - SendKeys到selenium VBA中打开对话框中的文件名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59415167/

相关文章:

vba - Excel 单元格默认度量单位

c# Selenium Webdriver - 元素隐藏 - "Element is not currently visible and so may not be interacted with"

java - 使用 Selenium 滚动到位于视口(viewport)内当前页面上的 Web 元素的最佳方法是什么

java - 使用 selenium WebDriver 自动化 Flipkart 应用程序

java - 如何使用 Apache POI (SXSSF) 设置特定单元格的数据(数字)格式区域设置?

c# - 使用c#检查excel中的分组行

excel - 将一个单元格的值分布到多个单元格上,直到剩余值为 0

vb.net - 通过 COM 接口(interface)向 VBA 公开 .NET DataTable 属性

excel - VBA Code Scraper 未将数据放置在右列中

excel - 如何使用 VBA 控制已经打开的 Web 文件对话框?