【问题标题】:From EXCEL VBA Click Button on Web从 EXCEL VBA 单击 Web 上的按钮
【发布时间】:2019-05-25 01:51:20
【问题描述】:

我在 Excel 中使用 VBA,并尝试访问网页,然后单击链接以在我的 PC 上触发 CSV 下载。

到目前为止,我可以使用 VBA 打开 IE 并访问所需的网页。但是在检查了页面的 HTML 代码 (F12) 并使用 getElementsByClassName 和其他方法之后,我无法生成一个可点击的对象来触发我的代码下载。

Sub Automate_IE_Enter_Data() '这将在 IE 中加载一个网页 暗淡我只要 将 URL 变暗为字符串 将 IE 调暗为对象 将 objElement 作为对象调暗 将 objCollection 调暗为对象 将 HWNDSrc 变暗

'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")

'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True

'Define URL
'URL = "https://www.automateexcel.com/excel/vba"
URL = "https://www.barchart.com/futures/quotes/ZWF9|530P/price-history/historical"

'Navigate to URL
IE.Navigate URL

' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."

' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertantly skipping over the second loop)
'Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop

'Webpage Loaded
Application.StatusBar = URL & " Loaded"

'Get Window ID for IE so we can set it as activate window
HWNDSrc = IE.hWnd
'Set IE as Active Window
SetForegroundWindow HWNDSrc

Set IEAppColl = IE.Document.getElementsByClassName("bc-glyph-download")(0)

IEAppColl.Click

Application.Wait Now + TimeValue("00:00:10")

SetForegroundWindow HWNDSrc

Application.Wait Now + TimeValue("00:00:05")

结束子

预期:1) 在 IE 上打开以下 URL: https://www.barchart.com/futures/quotes/ZWF9|530P/price-history/historical 2) 点击每日价格部分的“最高”链接下载 CSV 文件

1) 没问题 2) 产生错误:对象不支持此属性或方法。

关于如何在我的 VBA 代码中复制“最大”链接的点击,我没有点击。不确定我使用的是正确的名称。

【问题讨论】:

  • 请使用sn-p工具通过edit分享相关html

标签: excel vba web-scraping


【解决方案1】:

除非您已登录,否则该站点不会让您下载 csv。您指定的类链接到一个 jQuery,该 jQuery 会检查您的登录状态并在您未登录时引发错误。要通过 excel 下载它,您首先需要访问网站并创建一个帐户,然后编写代码让您登录,然后下载 csv,它应该可以工作。我不知道确切的语法,但希望这会为您指明正确的方向。

我还建议尝试 Selenium。它是一种流行的网络爬虫软件,因此应该很容易找到这方面的帮助。

【讨论】:

  • 还有一点将集合(例如getElementsByClassName(复数))视为单个元素。 getElementsByClassName("blah-blah")(0).click 可能是更好的方法。
  • 它与 getElementsByClassName("blah-blah")(0).click 一起工作正常!
  • 我现在可以访问网络并单击所需的“最大”按钮。如果我在运行代码之前登录到网络,那么新的 IE 窗口会自动登录,这很好。然后代码点击“max”按钮并触发下载Action。但是最后一个提示警告显示:“您要从 barchart.com 打开或保存“filename.csv”吗?我尝试使用 SendKeys 命令在该提示上选择 SAVE,以便它自动保存。不幸的是,Sendkeys 没有多大帮助作为 Tabs 在网络上不断变化。提示也在更改 IE 设置后出现...有什么想法吗??
  • @Alfred 尝试更改 IE 中的设置以在单击时自动下载文件,这样一开始就不会出现提示。
  • 简:IE11浏览器不允许。没有提示就无法单击以允许自动下载文件...一周尝试和博客只是为了节省您的时间。使用 Windows 10
【解决方案2】:

使用 IE - 您可以简单地使用以下命令启动下载,前提是未请求登录。

ie.document.querySelector("[data-ref=historical]").Click

更长的登录版本:

嗯,IE 和 Selenium 都引发了一次又一次的恐惧,包括登录详细信息不被接受(对于 IE)被检测为机器人、登录后引发的页面未找到错误、陈旧的元素引用、元素不可点击......以下是我为获得下载而成功发动的恐怖战争。明天早上我会看看我是否能找到更好的方法。

Option Explicit
Public Sub Download()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "https://www.barchart.com/futures/quotes/ZWF9%7C530P/price-history/historical"
    Const JS_WAIT_CLICKABLE = _
    "var target = this, endtime = Date.now() + arguments[0];" & _
    "(function check_clickable() {" & _
    "  var r = target.getBoundingClientRect(), x = r.left+r.width/2, y = r.top+r.height/2;" & _
    "  for (var e = document.elementFromPoint(x , y); e; e = e.parentElement)" & _
    "    if (e === target){ callback(target); return; }" & _
    "  if (Date.now() > endtime) { callback(target); return; }" & _
    "  setTimeout(check_clickable, 60);" & _
    "})();"                                      'by @florentbr

    With d
        .Start "Chrome"
        .get URL, timeout:=100000
        .FindElementByCss("[data-ref=historical]").Click

        With .FindElementsByCss("input.form-input")
            .item(1).SendKeys "name"
            .item(2).SendKeys "password"
        End With
        .FindElementByCss("[value=GO]").Click
        .GoBack
        .GoBack
        Application.Wait Now + TimeSerial(0, 0, 5)
        With .FindElementByCss("[data-ref=historical]")
            .ExecuteAsyncScript(JS_WAIT_CLICKABLE, 10000) _
        .Click
        End With
        .Quit
    End With
End Sub

【讨论】:

  • 嗨,我也试过 Selenium,但在安装 Selenium 类型库和 SeleniumWrapper 类型库后仍然无法正常工作。您能否将您正在使用的 ChromeDriver 的链接发送给我,以便我测试您的代码?谢谢!
  • 请注意正确的URL是:Const URL = "barchart.com/futures/quotes/ZWF|530P/price-history/historical",而且一旦你访问它就会变成一个稍微不同的URL地址,没用。
  • 在上面的代码中,您使用了不起作用的“转换后”URL:barchart.com/futures/quotes/ZWF9%7C530P/price-history/…
  • 您可以免费创建任何登录名和密码。一旦您在浏览器中登录,从头开始再次启动该浏览器会记住您的登录信息,并且不会提示您重新登录
  • 我测试并为我工作。我可以试试今天晚些时候更新的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
相关资源
最近更新 更多