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

问题描述 投票:0回答:1

我正在学习VBA。

我想静静地填写网页表格,例如

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

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

我想在任何操作系统上兼容它。我找到了这个

ActiveWorkbook.FollowLinkAddress "myurl.com"

但我不知道如何将它设置为变量之类的

Set IE = ActiveWorkbook.FollowLinkAddress "myurl.com"
IE.Visible = false

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

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

这是让我头痛的网址:

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

excel vba web-scraping webforms submit
1个回答
4
投票

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


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


你应该有一个适当的页面加载等待

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

在提交单击之后但在此处您还可以监视其中一个页面属性以获取指示已完成加载的更改(样式属性更改)


IE浏览器:

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等。安装硒后确保最新适用的驱动程序,例如ChromeDriver.exe位于selenium文件夹中,然后转到VBE>工具>参考>添加对Selenium类型库的引用。

硒与铬的示例:

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

确定默认浏览器:

如果您真的想编写一些复杂的代码来确定默认浏览器,您可以从注册表中检索它的详细信息,然后使用分支代码启动相应的浏览器(如果可以自动化)。您可以将以下快速测试示例更改为返回浏览器类型的函数。您需要安装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

将命令行更改为

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

返回progId。

示例返回:

enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.