1

I am trying to navigate through a website using a VBA macro. I have been successful so far adapting my code off of this answer, but have come upon a situation where I need to click on a "logout" link that does not have an ID or Name when I inspect it (for reference, see below:)

enter image description here

Through some searching, it seems that the preferred method to find and direct VBA to click on this link is using querySelector. I've found various similar questions (see here, here, here, and here) which direct how to use it.

As a test, I simply tried the below to see if it would work:

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

'log in, click on various elements here

    Set logoutButton = ie.document.queryselector("*")
    MsgBox logoutButton

Unfortunately, VBA throws Run Time Error 438 on the querySelector line, and I'm unable to understand what it is that I'm doing incorrectly.

I have added the following references to VBA:

vba references

Could someone let me know what it is I'm missing that's causing this error? Additionally, is this the method that I should be using to click the link? Thanks!

EDIT: @QHarr has been helpful with finding out why I'm getting this issue: it's a problem with the emulation settings on Internet Explorer. querySelector will only work with document mode 9 and beyond, but my IE is defaulting to 8.

Even when I change the mode to 9 (and enable the Persist Emulation setting), it appears that my IE still opens with 8 and only changes to 9 when I check on the document mode. This causes me to still get the same error, unless I manually pause my code, check the document mode, then rerun the code.

Salvaria
  • 51
  • 1
  • 9
  • 1
    Which OS version are you using and what is the emulation mode setting for internet explorer? – QHarr Aug 04 '22 at 20:20
  • @Qharr I'm using Windows Server 2012, Version 6.2 (Build 9200). How do I find which emulation mode setting I'm using? – Salvaria Aug 04 '22 at 20:36
  • https://learn.microsoft.com/en-us/deployedge/edge-ie-mode-faq – QHarr Aug 04 '22 at 21:18
  • @QHarr maybe I'm not following - my vba code appears to open Internet Explorer v 11.0.9600.20296 and does not seem to be Edge emulating a version of Internet Explorer – Salvaria Aug 04 '22 at 23:48
  • @QHarr I understand that IE is no longer supported by MS and is being phased out - I have not yet updated to Edge. Should I update? – Salvaria Aug 04 '22 at 23:56
  • You can use same instructions with IE to find emulation setting in browser. Your current wildcard css selector I would expect to match the root (document) node. – QHarr Aug 05 '22 at 04:18
  • @QHarr Thanks for the clarification - I'm using Document mode 8 via X-UA-compatible HTTP header – Salvaria Aug 05 '22 at 13:26
  • Change the mode to higher if possible to enable querySelector – QHarr Aug 05 '22 at 14:27
  • @QHarr I changed it to every higher option available and enabled the Persist Emulation setting but I am still getting the same error. I have the option to switch to 9, 10, or Edge. – Salvaria Aug 05 '22 at 14:48
  • Is the automated browser launching with that same setting? Or is it defaulting to prior setting? – QHarr Aug 05 '22 at 16:33
  • One way to test if this is the reason is to put the `STOP` command before the attempt to use querySelector then in the launched browser check the emulation mode, if <= 8 then set to higher than 8 and press F8 to execute next code line within the code pane of VBE and see if error still occurs. – QHarr Aug 05 '22 at 16:36
  • Worse case scenario, you either automate setting document mode or use a different method to target the node or even execScript to execute the associated javascript. There are plenty of options here. – QHarr Aug 05 '22 at 16:39
  • @Qharr it appears that if I set the mode to 9 in a previous window, the automated browser initially launches with 8 but switches to 9 when I check the emulation mode. If I stop the code, then check the emulation mode, then continue the code, it runs fine. Is there a way to force the automated browser to initiate in 9? – Salvaria Aug 05 '22 at 17:54
  • @Qharr if you post the information we chatted about as an answer to my post, I will happily mark it as the answer to my question :) – Salvaria Aug 09 '22 at 14:45
  • Did you manage to solve your problem? – QHarr Aug 10 '22 at 05:03
  • @QHarr - No, I wasn't able to force the automated browser to initiate in 9. However, you _did_ help me find out why querySelector wasn't working, so I appreciate that. If you know how to set the emulation mode in VBA, I would appreciate that also :) – Salvaria Aug 22 '22 at 15:24

1 Answers1

0

querySelector will throw an exception if nothing is selected. Try right clicking the element and copying the css selector. Or you can write your own.

table.LoginDialog td > a:nth-child(2) might work.

It's worth setting a break point on the selector and testing them out in the Immediate window.

SSlinky
  • 427
  • 2
  • 9
  • Couple of questions about your answer: 1) Shouldn't using the wildcard make querySelector find something? I thought that by using the wildcard, it should find something if it is working. 2) Since my code is throwing an error saying that InternetExplorer.Application doesn't support querySelector as a method, I don't think that it's even attempting to use querySelector yet, and that there's something else going wrong. – Salvaria Aug 05 '22 at 13:51
  • @Salvaria you're right - I assumed error 438 was "Object required" (which is what is thrown when nothing is found). Since you've added a reference to MS Internet Controls, try making use of early binding and `Dim ie As New InternetExplorer`. – SSlinky Aug 07 '22 at 01:36
  • @Salvaria your error `InternetExplorer.Application doesn't support querySelector` is interesting as you're not calling that method on it. You're calling it on `Document`. – SSlinky Aug 07 '22 at 01:37