1

I've created a script in vba using IE to click on a tab in a webpage. I would like to know how I can initiate a click on that tab using .execScript.

When I try like below, It works (not desirable approach):

Sub ExecuteScript()
    Dim IE As New InternetExplorer, Html As HTMLDocument

    With IE
        .Visible = True
        .navigate "https://stackoverflow.com/questions/tagged/web-scraping"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
        Html.parentWindow.execScript "document.querySelector(""a[href='/questions/ask']"").click();"
    End With
End Sub

The way I would like to do is the following so that I can use an object variable (adjacent or within) .execScript:

Set post = Html.querySelector("a[href='/questions/ask']")
Html.parentWindow.execScript "arguments[0].click();", post

But, it throws an error pointing at this line Html.parentWindow.execScript

Run-time error `429`
ActiveX component can't create object

How can I execute javascript within IE?

1 Answer 1

1

Why not change the variable type so you can pass post as string (i.e. the selector). Then you can concatenate in.

Option Explicit
Public Sub ExecuteAScript()
    Dim IE As New InternetExplorer, Html As HTMLDocument, post As String, item As Object
    post = "a[href='/questions/ask']"

    With IE
        .Visible = True
        .navigate "https://stackoverflow.com/questions/tagged/web-scraping"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
        Do
            On Error Resume Next
            Set item = .document.querySelector("" & post & "")
            On Error GoTo 0
        Loop While item Is Nothing

        If Not item Is Nothing Then item.Click

        Stop
    End With
End Sub

If you must use execScript I don't think you can pass back values with javascript return call as you can with selenium. You could add a value to the page with js and then read that back in order to return:

Option Explicit
Public Sub ExecuteAScript()
    Dim IE As New InternetExplorer
    post = "a[href='/questions/ask']"

    With IE
        .Visible = True
        .navigate "https://stackoverflow.com/questions/tagged/web-scraping"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Do
            Call .document.parentWindow.execScript("document.title = document.querySelectorAll(" & Chr$(34) & post & Chr$(34) & ").length;")
        Loop While .document.Title = 0
        If IsNumeric(.document.Title) And .document.Title > 0 Then Debug.Print "Success"
    End With
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I thought of it in the first place but the problem is that I wont be able to create any do loop or something like Do: Set post = Html.querySelector("a[href='/questions/ask']"): DoEvents: Loop While post Is Nothing to check for any element to be present before using this Html.parentWindow.execScript line. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.