Finding an Element in Chrome

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
fightcancer
Macro Veteran
Posts: 260
Joined: Fri Apr 15, 2005 8:32 am

Finding an Element in Chrome

Post by fightcancer » Mon Nov 25, 2024 5:44 am

Many of you probably figured out in a couple minutes what took me the entire weekend. For the rest, here's how I wasted 2 full days trying to find a single element.

Image

I was looking for the element, "CompExports", in Google Chrome. The HTML from Dev Tools is below. (Dev Tools are available by pressing F12 in Chrome, or right-clicking the relevant element on the webpage and left-clicking "Inspect" on the pop-up menu.)

Code: Select all

<td role="gridcell" style="" aria-describedby="clientList_ClientName">CompExports</td>
I had been searching using these 2 lines (and so very many others).

Code: Select all

ChromeFindElements>SID,xpath,//td[@role='gridcell'],elements

Code: Select all

ChromeFindElements>SID,xpath,//td[@aria-describedby='clientList_ClientName'],elements
By this point, I was already comfortable with switching frames (ChromeSwitchFrame>), and I found that this particular webpage had 6 of them. So I searched all 6 frames, and for good measure, wrote an SRT to help me search 200+ frames.

…and to make sure that each frame switch was successful (res=0).

…but the macro always ended with "elements_count=0".

So much frustration--but I pressed on. I scoured the page source code. I desperately searched the pane source code for any mention of frames, “src=” or other clues for the whereabouts of this elusive element. For the entire weekend, I sifted through hundreds of frames and even searched hundreds of frames within those frames!

No elements found. :cry:

I was totally ready to pay someone to help me find this annoying, aggravating needle in a haystack.

Finally, after more than 48 painful, progress-free hours…a breakthrough.

Glancing at my second monitor, my eyes just happened to land on an “<iframe>” tag in the Dev Tools code. Curious, I looked for a “src=” and found a partial URL. A new hope!

I tried the line

Code: Select all

ChromeFindElements>SID,xpath,//iframe[@src='/MS/Search/Export.cvs'],elements
Nothing.

I loaded the parameters into my custom SRT to search 200 frames for this iframe tag with src='/MS/Search/Export.cvs'. Match found in frame 6.

“Baby steps,” I thought to myself.

After some trial and error, I had:

Code: Select all

GoSub>SwitchFrames,5,ER5
ChromeFindElements>SID,xpath,//iframe[@src='/MS/Search/Export.cvs'],elements
ChromeSwitchFrame>SID,element,elements_1,res
    If>res<>0
      Mdl>Error switching to Export.cvs frame. res=%res%%CRLF%The macro will almost certainly fail.  Click OK to continue.
      **BREAKPOINT**
    EndIf
Then I tried again to find my evasive element

Code: Select all

ChromeFindElements>SID,xpath,//td[@aria-describedby='clientList_ClientName'],elements
Still nothing….

Custom SRT to search all the frames…. Match found in frame 0!

OMG! I knew I was so close! I had spent dozens of hours hunting this burdensome pest--and now it was within my grasp! I was overwhelmed with relief, excitement, hope and many feelings all at once! I literally ran into the other room to share the news with my gf.

I added a few more lines of code.

Code: Select all

GoSub>SwitchFrames,0,ER6
DelArray>elements
ChromeFindElements>SID,xpath,//td[@aria-describedby='clientList_ClientName'],elements
It worked!! After wasting an entire weekend chasing this evasive element, it worked! I had finally found it!

I added the last bit of code to parse the various elements and find the one I wanted.

Code: Select all

//Click the one that's named "CompExports."  Examine all found elements and get their names.
Let>k=0
Repeat>k
  //Increment the counter.
  Add>k,1
  ChromeGetElementData>SID,elements_%k%,attribute/innerHTML,res
  If>res=CompExports
    //This element is the one the macro should click.
    ChromeElementAction>SID,elements_%k%,click
    Goto>ClickNextOrMapping
  EndIf
Until>k,elements_count
Problem solved--and all I had to do was:

1) Learn that in this unique case, searching the page and pane source code wasn’t advantageous.
2) Search the code in Dev Tools for <iframe> tags.
3) Search all the webpage’s frames for the <iframe> in question.
4) Switch to that frame containing the pertinent <iframe>.
5) Search all the webpage’s frames for the desired element.
6) Switch to the relevant frame with that element.
7) Search for that specific element.
8. Parse the elements found for the one I wanted.

I was so happy to have conquered this large obstacle that had stood in my way for days! While I realize that for many, it was but a tiny puddle, for me it was a very perplexing puzzle that really challenged my critical thinking, creativity and commitment. I took some small pride knowing that this element was buried pretty deep compared to any other I’d chased, and writing the custom SRTs (see below) helped find the element more easily.

TL;DR
When searching for elements in Chrome (and possibly Edge), looking at the page or pane source code (Ctrl + U) may not yield any clues. It may be best to Inspect the element in question (twice), click the code in the Dev Tools and use Ctrl + F to find “<iframe” or similar--probably the instance immediately preceding the element you want. Then use ChromeFindElements and XPath to find that pane, ChromeSwitchFrame to switch to it and then search for your element. Example here.

fightcancer
Macro Veteran
Posts: 260
Joined: Fri Apr 15, 2005 8:32 am

Re: Finding an Element in Chrome

Post by fightcancer » Mon Nov 25, 2024 5:47 am

SRT to search through any number of frames.

Code: Select all

SRT>SearchFrames
/* Search through frame indeces for a particular element.

REQUIRED:
When calling this SRT, specify:
1) the number of indices to search.
2) the tag type, e.g. INPUT.
3) the attribute type, e.g. id.
4) the attribute value, e.g. 432.
5) whether to announce a match as soon as one is found.

Example:
GoSub>SearchFrames,200,input,id,432,1

Var "NoMoreFrames" means the macro believes there are no more frames when set to 1.  When =0, the macro will continue to
act as though more frames will be discovered and alert the first time a frame switch doesn't yield a successful result (a 
successful result will yield res=0).  So when NoMoreFrames=1, the macro will not alert when res<>0.
*/
GoSub>DisplayMessage,02,SRT>SearchFrames

//Assign vars.  Start the counter at -1 since the first index is the number zero.
Let>NoMoreFrames=0
Let>k=-1
Let>MaxTimesToSearch=%SearchFrames_Var_1%-1
//Search before switching frames.
DelArray>elements
ChromeFindElements>SID,xpath,//%SearchFrames_Var_2%[@%SearchFrames_Var_3%='%SearchFrames_Var_4%'],elements
If>elements_count>0
  If>%SearchFrames_Var_5%=1
    Mdl>Found w/o switching frames.%CRLF%%SearchFrames_Var_3%='%SearchFrames_Var_4%'
    **BREAKPOINT**
  EndIf
  Goto>EndSearchFrames
EndIf

Repeat>k
  Add>k,1
  //Don't use SRT>SwitchFrames here as I need more control over the outcome.
  ChromeSwitchFrame>SID,index,%k%,res
  If>res<>0
    //res<>0  Unsuccessful attempt to switch frames. Probably this frame doesn't exist.
    If>NoMoreFrames=0
      //The macro was expecting more frames but didn't find any.  Display a message and set NoMoreFrames=1.
      Mdl>SRT>SearchFrames failed switching to frame (k=)%k%.%CRLF%Click OK to continue.
      Let>NoMoreFrames=1
    Else
      //The macro was not expecting more frames.  Don't display a message.
    EndIf
    //Check the next frame.
    Goto>NextFrameInSearchFrames
  Else
    //res=0  Check the NoMoreFrames var.
    If>NoMoreFrames=0
      //The macro was expecting more frames and found more.  Don't display a message.
    Else
      //The macro was not expecting more frames.  Display a message and set NoMoreFrames=0.
      Mdl>SRT>SearchFrames switched to frame (k=)%k% believing there were no more frames.%CRLF%Click OK to continue.
      Let>NoMoreFrames=0
    EndIf
  EndIf

  DelArray>elements
  ChromeFindElements>SID,xpath,//%SearchFrames_Var_2%[@%SearchFrames_Var_3%='%SearchFrames_Var_4%'],elements
  ChromeSwitchFrame>SID,index,null,res
      If>res<>0
        //Failed to switch to parent frame.  Break execution after a message.
        Mdl>SRT>SearchFrames failed switching back to parent frame.  k=%k%.%CRLF%Click OK to continue.
        **BREAKPOINT**
        Goto>NextFrameInSearchFrames
      EndIf
  //If the macro found any eligible results, then copy the array to save the results.
  If>elements_count>0
    //elements_count > 0.  Save the results by copying the array "elements."  The new array will be named "Results%k%."
    ArrayDel>Results%k%
    ArrayCopy>elements,,Results%k%
    If>%SearchFrames_Var_5%=1
      Mdl>Found. Saved in Results%k%. (k=%k%)%CRLF%%SearchFrames_Var_3%='%SearchFrames_Var_4%'
      **BREAKPOINT**
    EndIf
  EndIf
  Label>NextFrameInSearchFrames
Until>k,MaxTimesToSearch

Label>EndSearchFrames
END>SearchFrames
Last edited by fightcancer on Mon Nov 25, 2024 4:54 pm, edited 2 times in total.

fightcancer
Macro Veteran
Posts: 260
Joined: Fri Apr 15, 2005 8:32 am

Re: Finding an Element in Chrome

Post by fightcancer » Mon Nov 25, 2024 5:49 am

Hopefully someone can learn from my mistakes and maybe make use of these SRTs, or offer some tips on my code.

Code: Select all

SRT>SwitchFrames
/* Switch to the frame specified when calling this SRT.  Then check the result to make sure it was successful.

REQUIRED
When calling this SRT, specify both 1) the frame to switch to, and 2) a description of where in the code this SRT was
called in case of an error.  The description can be anything unique, e.g. SMLS2, entering Tfla min., etc.

EXAMPLE
GoSub>SwitchFrames,null,SMLS3
*/
GoSub>DisplayMessage,02,SRT>SwitchFrames
ChromeSwitchFrame>SID,index,%SwitchFrames_Var_1%,res
    //Check the result.
    If>res<>0
      //The frame switch was unsuccessful.  Break execution.
      Mdl>Frame switch unsuccessful at %SwitchFrames_Var_2%.  Breakpoint if possible.  res=%res%
      **BREAKPOINT**
    EndIf

Label>EndSwitchFrames
END>SwitchFrames

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1414
Joined: Sun Nov 03, 2002 3:19 am

Re: Finding an Element in Chrome

Post by Dorian (MJT support) » Mon Nov 25, 2024 8:37 am

This was a nice post to wake up to after all the weekend head scratching. Glad to see you got there in the end. That frame turned out to be particularly elusive.

fightcancer
Macro Veteran
Posts: 260
Joined: Fri Apr 15, 2005 8:32 am

Re: Finding an Element in Chrome

Post by fightcancer » Mon Nov 25, 2024 3:09 pm

I'm so glad it's over! Ty for the good support--especially on a weekend!

User avatar
CyberCitizen
Automation Wizard
Posts: 724
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Re: Finding an Element in Chrome

Post by CyberCitizen » Tue Nov 26, 2024 8:20 am

Glad you got it sorted and got that thrill of the win.

And thank you for sharing your findings.
FIREFIGHTER

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts