在 MS Excel VBA 中静默填写 Web 表单

Fill web form in MS Excel VBA silently

我在学习VBA。

我想安静地填写网络表单,例如

Set IE = CreateObject("internet explorer.Application")
IE.VIsible = False

当我用这个加载 url 时,它说我应该使用另一个浏览器打开它。

我想让它兼容任何 OS。我找到了这个

ActiveWorkbook.FollowLinkAddress "myurl.com"

但我不知道如何将它设置为像

这样的变量
Set IE = ActiveWorkbook.FollowLinkAddress "myurl.com"
IE.Visible = false

然后我可以做一些事情,比如填写输入字段,点击按钮,.....

Set btn = IE.getElementById(...........
btn.Click = true

这是让我头疼的URL:

https://sellercentral.amazon.com/hz/fba/profitabilitycalculator/index?lang=en_US

使用超链接不是一个好方法。您希望以编程方式与网页进行交互,因此您需要一个自动浏览器。 IE 工作正常。我认为这是你写 internet explorer.Application 的错字,因为它是 InternetExplorer.Application.


注意:如果您决定通过安装 selenium basic 为不同的浏览器编写分支代码,我会在最后展示一些用于查找默认浏览器的代码。


您应该等待适当的页面加载

While .Busy Or .readyState < 4: DoEvents: Wend

点击提交后,您还可以在此处监视页面属性之一的更改,指示加载已完成(样式属性更改)


Internet Explorer:

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
'
Public Sub UseIE()
    Dim ie As New InternetExplorer
    With ie
        .Visible = False
        .Navigate2 "https://sellercentral.amazon.com/hz/fba/profitabilitycalculator/index?lang=en_US"

        While .Busy Or .readyState < 4: DoEvents: Wend

        .document.getElementById("search-string").Value = "1408893339"
        .document.querySelector("#a-autoid-1 .a-button-input").Click

        'While .Busy Or .readyState < 4: DoEvents: Wend

        Do
        Loop While .document.querySelector("#searchProduct").Style = "display: block;"

        Debug.Print .document.querySelector("#product-info").innerText

        Stop
        .Quit
    End With
End Sub

其他使用selenium的浏览器:

如果您想使用其他浏览器,请考虑 selenium basic vba,它将浏览器选择扩展到 Opera、Chrome、FireFox、PhantomJS 等。安装 selenium 后确保最新的适用驱动程序,例如ChromeDriver.exe 在 selenium 文件夹中,然后转到 VBE > 工具 > 参考 > 添加对 Selenium 类型库的引用。

示例硒 Chrome:

Option Explicit 
Public Sub EnterInfo()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "https://sellercentral.amazon.com/hz/fba/profitabilitycalculator/index?lang=en_US"

    With d
        .AddArgument "--headless"
        .Start "Chrome"
        .get URL
        .FindElementById("search-string").SendKeys "1408893339"
        .FindElementByCss("#a-autoid-1 .a-button-input").Click

        Do
        Loop While .FindElementByCss("#searchProduct").Attribute("Style") = "display: block;"

        Debug.Print .FindElementById("product-info").Text

        Stop                                     '<==delete me later
        .Quit
    End With
End Sub

正在确定默认浏览器:

如果您真的想编写一些复杂的代码来确定默认浏览器,您可以从注册表中检索它的详细信息,然后使用分支代码启动适当的浏览器(如果可以自动执行)。您可以将以下快速测试示例更改为 returns 浏览器类型的函数。您需要安装 selenium 才能使用 IE 以外的浏览器。

注意:使用 ProgID.

可能是更好的方法
Public Sub Test()
    Dim defaultBrowserInfo As String, browsers(), i As Long, found As Boolean, browser As String
    browsers = Array("Chrome", "InternetExplorer", "FireFox")

    defaultBrowserInfo = CreateObject("wscript.shell").exec("cmd /c REG QUERY HKEY_CLASSES_ROOT\http\shell\open\command").StdOut.ReadAll
    For i = LBound(browsers) To UBound(browsers)
        If InStr(defaultBrowserInfo, browsers(i)) > 0 Then
            found = True
            browser = browsers(i)
            Exit For
        End If
    Next
    If Not found Then
        MsgBox "Browser not in list supplied"
    Else
       MsgBox browser
    End If
End Sub

将 cmd 行更改为

REG QUERY HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\As
sociations\UrlAssociations\http\UserChoice

REG QUERY HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\As
sociations\UrlAssociations\https\UserChoice

returns progId.

示例return:

虽然使用 C# 有一个很好的代码结构 here