【发布时间】:2018-07-25 01:20:24
【问题描述】:
我正在 Excel VBA 中编写代码以获取类的 href 值并导航到该 href 链接
(即)这是href 值,我想进入我的特定 Excel 工作表,我想通过我的 VBA 代码自动导航到该链接。
<a href="/questions/51509457/how-to-make-the-word-invisible-when-its-checked-without-js" class="question-hyperlink">How to make the word invisible when it's checked without js</a>
我得到的结果是我能够得到包含标签的类值How to make the word invisible when it's checked without js href 链接/questions/51509457/how-to-make-the-word-invisible-when-its-checked-without-js 这是我想要得到并浏览我的代码的内容。
请帮帮我。提前致谢
下面是整个代码:
Sub useClassnames()
Dim element As IHTMLElement
Dim elements As IHTMLElementCollection
Dim ie As InternetExplorer
Dim html As HTMLDocument
'open Internet Explorer in memory, and go to website
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://stackoverflow.com/questions"
'Wait until IE has loaded the web page
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.document
Set elements = html.getElementsByClassName("question-hyperlink")
Dim count As Long
Dim erow As Long
count = 0
For Each element In elements
If element.className = "question-hyperlink" Then
erow = Sheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Sheets("Exec").Cells(erow, 1) = html.getElementsByClassName("question-hyperlink")(count).innerText
count = count + 1
End If
Next element
Range("H10").Select
End Sub
我在这个网站上找不到任何人问的任何答案。请不要将此问题建议为重复。
<div class="row hoverSensitive">
<div class="column summary-column summary-column-icon-compact ">
<img src="images/app/run32.png" alt="" width="32" height="32">
</div>
<div class="column summary-column ">
<div class="summary-title summary-title-compact text-ppp">
<a href="**index.php?/runs/view/7552**">MMDA</a>
</div>
<div class="summary-description-compact text-secondary text-ppp">
By on 7/9/2018 </div>
</div>
<div class="column summary-column summary-column-bar ">
<div class="table">
<div class="column">
<div class="chart-bar ">
<div class="chart-bar-custom link-tooltip" tooltip-position="left" style="background: #4dba0f; width: 125px" tooltip-text="100% Passed (11/11 tests)"></div>
</div>
</div>
<div class="column chart-bar-percent chart-bar-percent-compact">
100%'
【问题讨论】:
-
看起来你有 innerText 你需要 getAttribute("href")
-
如果我这样做,我不会得到空白单元格
-
还有其他办法吗?
-
@JeremyKahan 还有其他方法吗。
-
谁能解释一下下面的代码:
标签: html excel vba internet-explorer web-scraping