我创建了一个宏,该宏从网页中提取数据,然后将其打印到excel工作表中。该守则适用于:
打开网页并从页面转换为另一个
但问题是:
我不能点击Href链接来访问特定的页面
href的Html如下:
<a class=onglet href="/cmh/consultation/preViewMODScheduling.do">Scheduling</a>这个<a>的html容器是
<TD width=200><TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width=11><IMG src="/cmh/cmh/image/Tab1gh.gif" width=11
height=20></TD>
<TD background=/cmh/cmh/image/Tab1mat.gif align=center><A
class=onglet href="/cmh/consultation/preViewMODScheduling.do?
fromSelect=true">Scheduling</A></TD>
<TD width=11><IMG src="/cmh/cmh/image/Tab1dr.gif" width=11
height=20>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>我的代码:
Sub extt()
Dim IE As Object, obj As Object
Dim itm As IHTMLElement
Dim r As Long, c As Long, t As Long, d As Long
Dim lastRow As Variant
Dim elemCollection As Object
Dim ele As Object
Dim eRow As Long
Dim f As Variant
Dim oHtml As HTMLDocument
Dim MPNum As Variant
Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet
'add the microsoft Internet Controls
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "http://preSearchMOD.do?
clearBackList=true&CMH_NO_STORING_fromMenu=true"
While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
'we ensure that the web Page is loaded completely
Set itm = IE.document.getElementsByName("searchById")(0)
If Not itm Is Nothing Then itm.Value = Sheets("GDC").Range("C9").Value
Set doc = IE.document
Set tags = IE.document.getElementsByTagName("input")
For Each tagx In tags
If tagx.src = "htmh/image/button_search.gif" Then
tagx.Click
End If
Next
Set elemCollection = IE.document.getElementsByTagName("a")
Debug.Print elemCollection.Length
'Debug.Print elemCollection.body.innerHTML
For Each tagx In elemCollection
If tagx.href = "http/consultation/preViewMODScheduling.do?
fromSelect=true" Then
tagx.Click
Debug.Print tagx.body.innerHTML
End If
Next
myPoints = doc.getElementsByTagName("a")(3)
Range("A1").Value = myPoints
Application.Wait (Now + TimeValue("0:00:03"))
On Error Resume Next
While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
End With
Set IE = Nothing
MsgBox "Done"
End Sub此代码不返回任何内容,也不单击该href
Set elemCollection = IE.document.getElementsByTagName("a")
Debug.Print elemCollection.Length
'Debug.Print elemCollection.body.innerHTML
For Each tagx In elemCollection
If tagx.href = "http/consultation/preViewMODScheduling.do?
fromSelect=true" Then
tagx.Click
Debug.Print tagx.body.innerHTML
End If
Next此外,我还试图查看该HTML代码中有多少<a>,并且我发现76使用:Debug.Print elemCollection.Length
恢复:我想要的是到达那个<a>,然后点击那个href来获得调度页面。
有人能让我解决这个问题吗?
发布于 2018-03-29 15:37:32
在试图找到一个解决方案之后,我创建了这个,并且我知道我的代码是正确的。
Set elements = doc.getElementsByTagName("a")
For Each element In elements
If element.getAttribute("class") = "onglet" Then
If element.href = "Internal
PortXXXX/cmh/consultation/preViewMODScheduling.do?fromSelect=true" Then
element.Click
Exit For
End If
End If
Next element这个解决方案对我来说非常有效。
For Each l In Doc.getElementsByTagName("a")
If l.href = "h/cmh/consultation/preViewMODScheduling.do?fromSelect=true"
Then
l.Click
end if
Next诚挚的问候
马球
发布于 2018-06-10 05:28:34
带有CSS选择器
a.onglet[href*="/cmh/consultation/preViewMODScheduling.do?"]

"."说类onglet和"*" href包含/cmh....
代码使用情况:
doc.querySelector("a.onglet[href*=""/cmh/consultation/preViewMODScheduling.do?""]").Clickhttps://stackoverflow.com/questions/49554355
复制相似问题