【发布时间】:2019-02-26 20:23:58
【问题描述】:
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
Sub Pulldata_BI_Launch()
CreateObject("Shell.Application").Windows
Dim IE As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim elems As MSHTML.IHTMLElementCollection
Dim attr As MSHTML.IHTMLBodyElement
'Make internet explorer visible
IE.Visible = True
IE.Navigate "https://*************** "
ShowWindow IE.hwnd, 3
'Wait for page loads fully
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
'Collect webpage opened in variable
Set htmldoc = IE.document
'Click on Documents tab from container
Set elems = htmldoc.getElementsByTagName("a")
Set attr = elems.Item
For Each attr In elems
If (attr.Item("title") = "Documents") Then
attr.Click
Exit For
End If
Next attr
End Sub
我希望通过代码点击 Home 选项卡旁边的 Documents 选项卡,但无法这样做。任何帮助将不胜感激。以下是“文档”选项卡按钮的检查元素的屏幕截图: enter image description here
【问题讨论】:
-
如果你在
attr.Click这一行设置断点,你打到了吗? -
不要通过标签名称“a”获取所有元素,而是尝试使用 CSS 选择器
a[title='Documents']。这应该会让你得到你需要的,没有循环然后点击它 -
@matteo NNZ - 在断点模式下是的,当我调试它达到 attr.Click 但初始化为空值
-
@JeffC - 感谢您的快速建议,我今天一定会尝试使用 CSS 选择器。
标签: html excel vba internet-explorer web-scraping