访问iframe中的字段

时间:2019-06-06 11:15:59

标签: excel vba web-scraping

由于某种原因,我无法访问iframe中的元素。

我尝试过:

ie.Document.getElementById("sisaltosivu").contentDocument.getElementById("uusihaku")(0).Click

这是我的代码:

Sub ChechAutomate()

    Dim ie As New InternetExplorer, url As String, ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Other Data")

    url = "https://www.asiakastieto.fi/data/atdb"

    With ie
        .Visible = True
        .Navigate2 url

        While .Busy Or .ReadyState < 4: DoEvents: Wend

        With .Document

        If .querySelectorAll("#btnb").Length > 0 Then

        Else

            .querySelector("[name=user1]").Value = "x"
            .querySelector("[name=user2]").Value = "x"
            .querySelector("[name=passwd]").Value = "x"
            .querySelector("[class=btnb]").Click

            Application.Wait (Now + TimeValue("00:00:01"))

            ie.Navigate2 "https://www.asiakastieto.fi/sopimusasiakas/kotimaa?lang=FI"

            Application.Wait (Now + TimeValue("00:00:01"))
             ie.Document.getElementById("sisaltosivu").contentDocument.getElementById("uusihaku")(0).Click

        End If

        End With

    End With

End Sub

enter image description here

1 个答案:

答案 0 :(得分:2)

当您导航到iframe的src时,您将不再位于iframe之外。这行:

ie.Navigate2 "https://www.asiakastieto.fi/sopimusasiakas/kotimaa?lang=FI"

应该意味着您现在位于iframe中引用的文档中,并且应该直接通过以下方式进行访问:

.document.getElementById("uusihaku")(0).Click