1

I am trying to automate the login on a website. So far I managed to let the script type in a user-name and password and also click on a login button. This works just fine and I am logged into the website. Next step would be to click on some links to get to the right page where I can enter the search data the site needs to find the data for me.

This is the HTML-code of the tag involved:

<a title="Klik hier voor de dienst Kadaster-on-line" class="serviceAvailable" href="https://kadaster-on-line.kadaster.nl/default.asp" target="_self" ng-if="!menuItem.items" ng-repeat-start="menuItem in menuItems">Kadaster-on-line</a>

I have been trying to get VBA to click on a link but can't get in right. This is my latest attempt:

Set alle_keuzes = IE.document.getElementsByTagName("a")

For Each keuze_voor_kadaster In alle_keuzes        
    If keuze_voor_kadaster.getAttribute("title") = "Klik hier voor de dienst Kadaster-on-line" Then
        keuze_voor_kadaster.Click
        Exit For
    End If
Next keuze_voor_kadaster

What would be a proper way to do this?

1
  • What does can't get it right mean? Is there an error? Does nothing happen? Is there a parent frame/iframe surrounding your target element? Commented Nov 15, 2018 at 13:43

1 Answer 1

2

The link should be selectable by

ie.document.querySelector("a[title='Klik hier voor de dienst Kadaster-on-line']").click

You could also try:

ie.document.querySelector("a[title='Klik hier voor de dienst Kadaster-on-line']").FireEvent "onclick"

If you are getting an object not set/found error then:

1) Inspect the html to see ifparent frame/iframe tag which you element is found inside of.

In that case you may need syntax similar to:

ie.document.document.getElementsByTagName("frame")(frameIndexGoesHere).contentDocument.querySelector("a[title='Klik hier voor de dienst Kadaster-on-line']")

Use getElementById if the frame/iframe has an id.

2) Check you are waiting long enough for the element to be present before clicking. If that is the case ensure you have the following for page load and a loop to wait for element to be present:

After navigating to URL

While ie.Busy Or ie.readyState < 4: DoEvents: Wend

Const MAX_WAIT_SEC As Long = 5
Dim ele As Object, t As Date
t = Timer
Do
    DoEvents
    On Error Resume Next
    Set ele = ie.document.querySelector("a[title='Klik hier voor de dienst Kadaster-on-line']")
    On Error GoTo 0
    If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing

If Not ele Is Nothing Then ele.Click
Sign up to request clarification or add additional context in comments.

17 Comments

The first two solutions provided a 424 error (object required). So it seems that I should indeed have a wider look at the HTML code.
I made a few typos which I have now corrected. Try the bottom loop version and and also check for parent frame/iframe tag. If you can copy the entire page HTML into a pastebin.com (if not private content) I can inspect that.
I did this little project in Python at first and now, since this is no longer supported, I have to do this in VBA.
I have managed to get my code to work. In the end I used this code:
QHarr: I am not quit sure but I am glad to see that everything works just fine. I am not really into VBA but this task tiggered me to spent a lot more time into VBA. This is fun too. I suppose my question was more super basic for you. Thanks to all the people above I learned quite a bit.
|

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.