if语句使用getelementbyclass

时间:2013-05-21 03:49:39

标签: vba if-statement getelementsbyclassname

首先,我对编程比较陌生。我一直熟悉VBA。我之前在这里提出过问题,到目前为止,回复非常有帮助,所以我再次尝试运气。

我要访问此网站:http://www.otcmarkets.com/stock-screener

我希望我的脚本说出类似的内容:

if ElementClassName("listingOdd").innertext = "USA, NV"
then GetElementsbyClassName("listingOddlistingOdd whiteSpaceNormal").innertext

我还想循环浏览并单击此html后面的下一个按钮,直到它完成:     a href =“javascript:void(null);”> next& GT; / A

欢迎任何帮助,我一直在搜索过去的问题和观看教程,并且不会问我是否认真对待这个问题。非常感谢提前

这是我尝试过的一些代码,出现以下错误:“对象不支持此方法” 从试错中有很多评论

Sub t()


Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False

IE.navigate "http://www.otcmarkets.com/stock-screener"

Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.document
Dim sDD As String
Dim lnk As Variant
sDD = Trim(Doc.GetElementbyClassName("listingOddlistingOdd whiteSpaceNormal").innerText)
'If lnk.classame.innerText = "USA, NV" Then
If lnk.innerText = "USA, NV" Then
'If ElementClassName("listingOdd")(0).innerText = "USA, NV" Then
'sDD
'sDD = Trim(Doc.GetElementbyClassName("listingOddlistingOdd whiteSpaceNormal").innerText)
MsgBox sDD
End If




End Sub

抱歉意大利面条代码,就像我说我是新的

1 个答案:

答案 0 :(得分:1)

我不清楚你到底想要得到什么(为什么不只是使用页面顶部方便的“全部下载”链接?)

已编辑 - 将单元格值放在工作表上 EDIT2 - 点击“下一步”链接。 EDIT3 - 循环播放

这种东西虽然是一个完整的兔子洞。

Sub Tester()
Dim doc As Object
Dim IE As Object, nxt
Dim rng As Range, x As Integer

    Set rng = ThisWorkbook.Sheets("sheet1").Range("A2")
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.navigate "http://www.otcmarkets.com/stock-screener"
    WaitForLoad IE

    Set nxt = GetNext(IE)
    Do While Not nxt Is Nothing
        ExtractTableContent IE, rng
        nxt.Click
        WaitForLoad IE
        Set nxt = GetNext(IE)
    Loop
End Sub

Sub WaitForLoad(IE As Object)
    Do: DoEvents: Loop Until IE.readyState = 4 'READYSTATE_COMPLETE
    Application.Wait Now + TimeSerial(0, 0, 3)
End Sub
'copy table content
Sub ExtractTableContent(ByRef IE As Object, ByRef rng As Range)
    Dim tableDiv As Object, r As Object, rw As Object, x As Long

    Set tableDiv = IE.document.getelementbyid("stockScreenerResults")
    Set r = tableDiv.getelementsbytagname("table")(0).Rows
    For Each rw In r
        For x = 1 To rw.Cells.Length
            rng.Offset(0, x - 1).Value = rw.Cells(x - 1).innerText
        Next x
        Set rng = rng.Offset(1, 0)
    Next rw
End Sub

'find the link which takes you to the next page...
Function GetNext(IE As Object) As Object
    Dim links, l As Object, rv As Object, cn As Object
    Set links = IE.document.getelementsbytagname("a")
    For Each l In links
       If l.innerText Like "*next*" Then
        Set rv = l
        Exit For
       End If
    Next l
    Set GetNext = rv
End Function