excel - 在 MS Excel VBA 中静默填写 Web 表单
问题描述
我正在学习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
解决方案
使用超链接不是一个好方法。您希望以编程方式与网页交互,因此您需要一个自动浏览器。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 等。安装 selenium 后,确保最新的适用驱动程序,例如 ChromeDriver.exe 在 selenium 文件夹中,然后转到 VBE > 工具 > 参考 > 添加对 Selenium 类型库的引用。
使用 Chrome 的 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
将 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
返回 progId。
示例返回:
虽然使用 C#,但这里有一个很好的代码结构。
推荐阅读
- reactjs - 为什么地图在 Meteor 的 Tracker.autorun 下不起作用?
- jquery - 如何使用 DropZone 插件 Laravel JS 编辑和上传现有图像
- c++ - 即使在Linux中使用'pkg-config --cflags --libs opencv`也无法用openCV编译c++
- c# - 如何使用 C# 中的反射将不带参数的 void 方法订阅到运行时位于不同类中的静态事件(Action)
- flutter - Dio 没有响应关闭的 API
- vue.js - 这个出席频道中的“聊天”和 echo.join 中的“聊天”是什么?
- python - 优化 Pyomo Python - 'SumExpression 不可迭代'
- javascript - TypeError:无法将属性“innerHTML”设置为 null
- flutter - Riverpod 不需要的小部件重建(我认为)
- android - 如何在 android 上使用 kotlin 处理 OnBackPressed