asguldbrandsen
asguldbrandsen

Reputation: 173

Click a button in IE using VBA

I am trying to automate a download from a webpage, and it requires that i klik a button to access a login form.

I have searched the web and various threads for answers, with some luck, but now I get the error:

'Object doesnt support this property or method'.

The HTML part for the button i want to click is:

<button name="idpEndpointId" type="submit" value="https://saml.nemlog-in.dk"   class="btn btn-primary">Vælg</button>

Any suggestions?

Private Sub download()

Dim IE As Object
Dim document As Object
Dim nemKnap As Object

'Create IE object
Set IE = CreateObject("InternetExplorer.Application")

'Show browser
IE.Visible = True

'Navigate to URL
IE.Navigate "https://link.com/"

'Statusbar if waittime
Application.StatusBar = "Page is loading."

'Wait
'Application.Wait Now + TimeValue("00:00:02")

' First attempt on tabbing through the website to press enter
  '  SendKeys "{TAB}"
  '  SendKeys "{TAB}"
  '  SendKeys "{TAB}"
  '  SendKeys "{TAB}"
  '  SendKeys "{TAB}"
  '  SendKeys "{TAB}"
  '  SendKeys "{TAB}"

'Do
'Loop While IE.busy

Do
Loop Until IE.readystate = 3
Do
Loop Until IE.readystate = 4

Set nemKnap = IE.document.getElementsByValue("https://saml.nemlog-in.dk")

'nemKnap.Click // is supposed to happen afterwards

End Sub

Upvotes: 0

Views: 5225

Answers (2)

QHarr
QHarr

Reputation: 84465

Or use CSS queryselector

Option Explicit

Public Sub ClickButton()

    Dim IE As New InternetExplorer, html As HTMLDocument

    With IE
        .Visible = True
        .navigate "https://kommune.bbr.dk/IdPSelection#!/"
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set html = .document
    End With

   html.querySelector("button.btn.btn-primary[value=""https://saml.nemlog-in.dk""]").Click

   Stop   '<== delete this line later. This is just to enable you to observe result.
   '<Other code>
   .Quit

End Sub

Upvotes: 1

Ricardo A
Ricardo A

Reputation: 1815

The link you provided has the button you mentioned but the value is different.

<button name="idpEndpointId" type="submit" value="https://saml.adgangsstyring.stoettesystemerne.dk" class="btn btn-primary">Vælg</button>

There are a few ways to click this button. The easiest way I see for your specific situation is to click the 3rd button. The website has 3 buttons and you can do the following:

IE.document.getElementsByTagName("button")(2).Click

The array of buttons starts at 0, that's why 2 is the 3rd button.


Another way to do this, is to loop all the buttons and find the one you want:

Set allButtons = IE.document.getElementsByTagName("button")
Do While i < allButtons.Length
    If allButtons(i).Type = "submit" And allButtons(i).Value = "https://saml.adgangsstyring.stoettesystemerne.dk" Then
        allButtons(i).Click
        Exit Do
    End If
    i = i + 1
Loop

Upvotes: 2

Related Questions