excel - 如何让我的 VBA 网页抓取代码跟上新窗口的速度?

标签 excel vba internet-explorer web-scraping automation

我在我的 VBA 代码中打开了一个新链接,它只在新窗口中打开,因此代码无法跟随新窗口。

我很难过这是我的代码。

<div class="table-r">
  <table class="table-c">
    <tbody>
      <tr class="bg">
        <td class=" np">    <a title="comm" href="logcomm-e.asp?id=3" rel="history">3</a> </td>
        <td class=" np">    <a title="comm" href="logcomm-e.asp?id=2" rel="history">2</a> </td>
        <td class=" np">    <a title="comm" href="logcomm-e.asp?id=1" rel="history">1</a> </td>
      </tr>
    </tbody>
  </table>
</div>

Sub IEScrape()
  Dim IE As Object
  Set IE = New InternetExplorerMedium

    With IE
         .Visible = True
         .Navigate "inhouse web site"

         While IE.readyState <> 4
             DoEvents
         Wend

         .document.querySelector("a[href*='logcomm-e.asp?id=']").Click

         While IE.readyState <> 4
             DoEvents
         Wend

    End With

  Set IE = Nothing
  ActiveWorkbook.Save

End Sub

我希望当我运行 get 元素并将其打印到来自新 href 链接的单元格时。我不能直接访问链接,因为每天都有一个新链接,并且 href 获取更新。通过使用 querySelector 它点击最新的条目。目前是明天 4 点 3 点,以此类推。

最佳答案

获取 .href querySelector 的属性(property)然后导航到它,应该可以工作:

With IE
     Dim hrefString as String
     hrefString = .document.querySelector("a[href*='logcomm-e.asp?id=']").href
     .Navigate = hrefString
End With

关于excel - 如何让我的 VBA 网页抓取代码跟上新窗口的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56585498/

相关文章:

javascript - 无法在 IE/Edge 中执行 Array.prototype.find

jquery - 为什么 jQueryeach() 不能在 Internet Explorer 中触发?

java - 无法使用 Selenium 在 IE 11 的关闭窗口中找到元素

excel - 这不是我的问题的描述,但是Stackoverflow不允许我提交我的描述性标题

vba - Excel VBA : SendKeys fails on some computers

excel - 运行 n 行的计算函数

vba - 通过十六进制编辑器的 Excel VBA 密码

c# - 如何设置Excel窗口的大小?

excel - 如何编写一个excel公式来根据单元格的内容从一个范围内返回一个值?

Excel 组合框刷新下拉菜单?