0

I am trying to automate one of my website working on a secured server, the code which help me to log into the web page but after that I have to click a dropdown from a menu bar where the HTML tag available is CLASS and HREF only.

HTML

<li>  
<a Class = "childmenu" href="./1713363899/ViewLogsPage">  
<span>Logs</Span>  
</a>  
</li>

A part of my VBA code is

Dim MyHTML_Element as IHTMLElement 
Set HTMLDoc = MyBrowser.document


For Each MyHTML_Element In HTMLDoc.getElementsByClassName ("childmenu") 
If MyHTML_Element.href = "./1713363899/ViewLogsPage" Then MyHTML_Element.Click:  
Exit For  
Next  

but this code not working and Skip after executing this line ".....ClassName ("childmenu")" and jump to END sub, when i run my code in Debug mode.

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44
mithun nair
  • 136
  • 11
  • Oh my... posting errors as pictures is already wrong way of doing it, but taking a picture of your screen... Try to find snipping tool in your windows, or install for example GreenShot – James Z Nov 15 '17 at 15:11
  • code is already there in my post, and i put this screenshot to describe/show that the code just jump from yellow line to END SUB with out executing or touching other line...wondering why this is happening any way thanks @James Z – mithun nair Nov 15 '17 at 15:15

2 Answers2

1

Try

If Instr(MyHTML_Element.href, "1713363899/ViewLogsPage") Then MyHTML_Element.Click

If not works, add debug.print MyHTML_Element.href before line with If. It is possible that link is generated while page is being download.

MarcinSzaleniec
  • 2,246
  • 1
  • 7
  • 22
  • Thanks @MarcinSzaleniec I tried `For Each MyHTML_Element In HTMLDoc.getElementsByClassName ("childmenu") debug.print MyHTML_Element.href If Instr(MyHTML_Element.href, "1713363899/ViewLogsPage") Then MyHTML_Element.Click` but they are not working – mithun nair Nov 15 '17 at 14:26
  • i tried both the code as u mentioned with and with out Debug print (i dont used it before). But I can see the code is not even executing any comments after 1st line `For Each MyHTML_Element In HTMLDoc.getElementsByClassName ("childmenu")`, it just jump to End sub – mithun nair Nov 15 '17 at 14:41
  • Debug.Print prints value of its argument in the Immediate Window. You can show Immediate Window by shortctu ctrl+G, it is very useful. I suspect that your for each loops through 0 elements. To check it you may add 'debug.print Ubound(HTMLDoc.getElementsByClassName ("childmenu") )'. – MarcinSzaleniec Nov 15 '17 at 14:48
  • it says expected array – mithun nair Nov 15 '17 at 15:01
  • I expected it too. HTMLDoc.getElementsByClassName ("childmenu") should be an array of HTML elements, shouldn't it? Now I think that something wrong must in in your earlier code. – MarcinSzaleniec Nov 15 '17 at 15:04
  • My code have only 2 part, Part 1: login to a webpage (completely operates in a secured server and a intranet). And the Part 2: is to Navigate to a dropdown page by clicking the tab from the Menu bar. Where My 1st part is working perfectly fine which means I can login to the web using my VBA code, now the very beginning of part2 is getting failed its like after login the desired tab is not getting clicked... and the code gets ended – mithun nair Nov 15 '17 at 15:07
0

Use a CSS selector of .childmenu[href='./1713363899/ViewLogsPage']. Which reads as the elements with classname childmenu having attribute href with value './1713363899/ViewLogsPage'


CSS selector in action:

css query


VBA:

You apply the CSS selector with the .querySelector method of the .document

MyBrowser.document.querySelector(".childmenu[href='./1713363899/ViewLogsPage']").Click

Info on:

  1. CSS selectors
  2. .querySelector method

Bonus track:

Feast on CSS selectors to home your CSS ninja skills - I have no affiliation with this. Found it today and it was fun.

Or try the tester via this: CSS Selector Reference

QHarr
  • 83,427
  • 12
  • 54
  • 101