Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何让我的VBA web抓取代码跟上新窗口?_Excel_Vba_Internet Explorer_Web Scraping_Automation - Fatal编程技术网

Excel 如何让我的VBA web抓取代码跟上新窗口?

Excel 如何让我的VBA web抓取代码跟上新窗口?,excel,vba,internet-explorer,web-scraping,automation,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">

我在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>


副主席()
模糊的物体
设置IE=新的InternetExplorerMedium
与IE
.Visible=True
.浏览“内部网站”
而IE.readyState 4
多芬特
温德
.document.querySelector(“a[href*='logcomm-e.asp?id='])。单击
而IE.readyState 4
多芬特
温德
以
设置IE=无
活动工作簿。保存
端接头

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

获取
查询选择器的
.href
属性,然后导航到它,应该可以:

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

使用类和类型选择器(但+到@vityta)来获取href并导航到它会稍微快一点。QuerySelector返回第一个匹配项,它将是您案例中最新的匹配项

ie.Navigate2 .document.querySelector(".table-c a").href

作为解决办法-您能否获取新窗口的href地址并通过
IE.Navigate newAddress
打开它?它应该能工作,而且我想如果你能正确地管理结束,它会是可靠的。我不能,因为它得到了最新的链接,每天都有一个新的链接。今天是#3明天是#4等等。谢谢你的建议。我不是说“硬编码”,我的意思是从打开的页面上读
href
,然后去那里。看到这个问题了-哦,好吧,我之前也试过类似的方法,但没用,但现在可以用了,谢谢!是的,我刚刚做了这个
.Navigate(.document.querySelector(“a[href*='logcomm-e.asp?id=']).href)
基本上你的建议再次感谢。
ie.Navigate2 .document.querySelector(".table-c a").href