Home » excel » excel – Access fields inside iframe

excel – Access fields inside iframe

Posted by: admin May 14, 2020 Leave a comment

Questions:

For some reason I am not able to access elements inside iframe.

I have tried:

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

Here is my code:

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

How to&Answers:

You are no longer outside the iframe when you navigate to the src of the iframe. This line:

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

should mean you are now in the document referenced within the iframe and should access direct with:

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