1

I'm trying to automate the login at www.tdameritrade.com and am able to fill out the username and password using:

IE.Document.getElementById("userid").Value = "username"
IE.Document.getElementById("password").Value = "password"

The login button doesn't have an Id.

How can I click the button when the code for it is:

<button tabindex="5" title="Log in" class="main-header-login-submit btn btn-green-solid" type="submit" href="https://invest.ameritrade.com/grid/p/login" value="Log in" target="_self"></button>
Community
  • 1
  • 1
bolambert
  • 19
  • 2
  • do any of these help: https://stackoverflow.com/questions/42262485/click-in-a-button https://stackoverflow.com/questions/28153744/use-excel-vba-to-click-on-a-button-in-internet-explorer-when-the-button-has-no https://stackoverflow.com/questions/22670379/excel-vba-ie-clicking-a-button – Scott Craner Jul 21 '17 at 19:34
  • Is the button part of a form? – Mike Jul 21 '17 at 19:37
  • use `GetElementsByClassName` and iterate that collection with additional logic if needed, there's probably only one item that matches that query, but if there's more than one, you need to figure out which one to "click". – David Zemens Jul 21 '17 at 19:42

2 Answers2

1

Try this:

Dim divClassLogin As Object

IE.Document.getElementById("userid").Value = "username"
IE.Document.getElementById("password").Value = "password"

Set divClassLogin = IE.Document.getElementsByClassName("main-header-login-fields")

divClassLogin(0).Children(2).Click
Christian
  • 11
  • 4
  • What is the reasoning for the Children(2) please ? I guess it DOM model but how does that fit with what is shown in the question? Genuine question. – QHarr Apr 11 '18 at 19:59
  • If you look at the source code on td ameritrade home page, and find the div element with class name "main-header-login-fields", you will see that it contains 3 children elements: 2 inputs and 1 button. Children(2) refers to the login button. The original question was: "How can I click the button". This is the answer. – Christian Apr 11 '18 at 20:08
  • Aha! That would make total sense. – QHarr Apr 11 '18 at 20:08
  • Done! Thx @Adam – Christian Apr 11 '18 at 20:12
  • @bolambert: if this answer is helpful, please mark as solved. Thx! – Christian Apr 13 '18 at 16:29
1

A simply CSS attribute selector will do of [title='Log in']. The [] is for attribute. Inside is the attribute = value combination.

The selector is applied via the .querySelector method of document

ie.document.querySelector("[title='Log in']").Click
QHarr
  • 83,427
  • 12
  • 54
  • 101