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