Bra*_*ech 0 excel vba webforms submit web-scraping
我正在学习VBA。
我想默默地填写网络表格,例如
Set IE = CreateObject("internet explorer.Application")
IE.VIsible = False
Run Code Online (Sandbox Code Playgroud)
当我用它加载网址时,它说我应该使用另一个浏览器打开它。
我想让它在任何操作系统上兼容。我找到了这个
ActiveWorkbook.FollowLinkAddress "myurl.com"
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将其设置为变量,例如
Set IE = ActiveWorkbook.FollowLinkAddress "myurl.com"
IE.Visible = false
Run Code Online (Sandbox Code Playgroud)
然后我可以做一些事情,比如填写输入字段,单击按钮,......
Set btn = IE.getElementById(...........
btn.Click = true
Run Code Online (Sandbox Code Playgroud)
这是让我头疼的网址:
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
Run Code Online (Sandbox Code Playgroud)
提交点击后,但在这里您还可以监视页面属性之一是否发生指示加载已完成的更改(样式属性更改)
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
Run Code Online (Sandbox Code Playgroud)
其他使用selenium的浏览器:
如果您想使用替代浏览器,请考虑selenium basic vba,它将浏览器选择扩展到 Opera、Chrome、FireFox、PhantomJS 等。安装 selenium 后,确保最新的适用驱动程序(例如 ChromeDriver.exe)位于 selenium 文件夹中,然后转到 VBE > 工具 > 引用 > 添加对 Selenium 类型库的引用。
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
Run Code Online (Sandbox Code Playgroud)
确定默认浏览器:
如果您确实想编写一些复杂的代码来确定默认浏览器,您可以从注册表中检索它的详细信息,然后使用分支代码启动适当的浏览器(如果可以自动化)。您可以将以下快速测试示例更改为返回浏览器类型的函数。您需要安装 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
Run Code Online (Sandbox Code Playgroud)
将命令行更改为
REG QUERY HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\As
sociations\UrlAssociations\http\UserChoice
Run Code Online (Sandbox Code Playgroud)
或者
REG QUERY HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\As
sociations\UrlAssociations\https\UserChoice
Run Code Online (Sandbox Code Playgroud)
返回 progId。
返回示例:
尽管使用 C#,但这里有一个很好的代码结构。