1

In VBA I wondered if this possible to click on this HTML button without Id and Name attribute :

<ul aria-hidden="false" role="menu">
   <li>
        <button ..></button>
   </li>
   <li>
        <button ..></button>
   </li>
   <li> <!-- THE THIRD li, I would like to access this button -->
        <button title="exporter" role="menuitem" data-ng-disabled="myfunction()">my text</button>
  </li>
</ul>

For other elements with Id or a Name, here is how I do :

Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "site.com"
IE.document.getElementById("submitBtn").Click
// IE.document.getElementByName("submitBtn").Click

but in my previous example there is no "Id" or "Name" available, only a Title attribute for the button, is there any possibility to access this button ?

Any idea ?


Another way I tried but also not working :

the goal is to click the button, so I tried to execute directly "myfunction()", like this :

IE.document.parentWindow.execScript "myfunction()", "Javascript"

btu I get an execution error (error 80020101).. any idea ?

Julien
  • 3,743
  • 9
  • 38
  • 67
  • Yes, you can specify it as the nth child (childNodes[]) of an element that does have an id or name. – Alex K. Jun 03 '16 at 14:46
  • if the HTML is valid XML, you could also use XPath to select nodes/attributes based on attributes, position or sibling. – ThunderFrame Jun 03 '16 at 14:48
  • Or GetElementsByTagname and loop through the returned collection until you find one with the correct title attribute. – Tim Jun 03 '16 at 14:55
  • thank you, and is it possible for example to access the button where "myfunction()" has been located, I mean, "myfunction" is call only there so is there a way to get the node directly from that? – Julien Jun 03 '16 at 14:56
  • @Tim, yes good idea but the title can change, I mean, it depends the language of the page, so indeed even this title I cannot use.. – Julien Jun 03 '16 at 15:00
  • 1
    And just a heads up (in anticipation of the next question). That site is using AngularJS, so if `myfunction()`is `TRUE`, the button will be disabled and unclickable. I've never tried to programmatically change and AngularJS attribute through IE Automation, so I'm not 100% sure if you can just set `data-ng-disabled=FALSE` or if you will have to force a false evaluation of myfuction(). – Tim Jun 03 '16 at 15:01
  • If the title is inconsistent, then you will have to find something that is. Eg, it is always the only button in the 3rd li, or it is the only button who's role is menuitem, or it is always the last button on the page. So long as there is one consistent thing (and there must be something), you can select that element. In an absolute worst case, you could investigate creating your own [`POST`](http://stackoverflow.com/questions/1820345/perform-http-post-from-within-excel-and-parse-results) – Tim Jun 03 '16 at 15:06
  • @Tim, maybe a stupid question, but how do you know that the site is using Angular? – Julien Jun 03 '16 at 15:06
  • @Julien from the [`ng` directives](https://docs.angularjs.org/api/ng). – Tim Jun 03 '16 at 15:08
  • @Tim, thank you very much Tim! – Julien Jun 03 '16 at 15:08
  • do you need to use vba? [imacros](https://addons.mozilla.org/en-US/firefox/addon/imacros-for-firefox/) will accomplish this easily – jellz77 Jun 03 '16 at 15:20
  • thanks but not my need, I don't want to move automately a cursor, and people complain about this addon – Julien Jun 03 '16 at 15:26

1 Answers1

1

Have you tried to loop through the elements and see a possible way to retrive how it is being detected?

For Each element In IE.Document.all
If element.className = "exporter" Then
Set LookedElement = element 
Exit For
End If
Next

Other useful feature to set elements

Set elems = IE.Document.getelementsbytagname("exporter")
Set elems = IE.Document.getElementsByName("exporter")

It may be more useful to add the reference (References->Microsoft Internet Controls) so you can see all the commands available for IE instead of late binding.

Sgdva
  • 2,800
  • 3
  • 17
  • 28
  • 1
    yes and for those who didn't see this reference like me before, shdocvw.dll is simply missing, import it from your system32 files – Julien Jun 03 '16 at 15:01