[Solved]Cannot access frame attributes

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

[Solved]Cannot access frame attributes

Post by ocnuybear » Thu Sep 15, 2022 7:28 am

Hi everybody I have this code below that finds 5 frames on this website, but does not switch to it succesfully, maybe because it is a frame not an Iframe:

http://the-internet.herokuapp.com/nested_frames

Code: Select all

Let>CHROMEDRIVER_EXE=D:\Downloads\chromedriver.exe

ChromeStart>session_id

ChromeNavigate>session_id,url,http://the-internet.herokuapp.com/nested_frames

ChromeFindElements>session_id,tag name,frame,frmlev1

Let>x1=0
While>x1<frmlev1_count
  Let>x1=x1+1
  ChromeSwitchFrame>session_id,element,frmlev1_1,res
  //ChromeGetElementData>session_id,frmlev1_1,attribute/cd_frame_id_,frm1_str
  //ChromeGetElementData>session_id,frmlev1_1,attribute/outerHTML,frm1_str
  ChromeGetElementData>session_id,frmlev1_1,attribute/name,frm1_str
  ChromeFindElements>session_id,tag name,frame,frmlev2
  MessageModal>frm1_str
  Let>x2=0
  While>x2<frmlev2_count
    Let>x2=x2+1
    ChromeSwitchFrame>session_id,element,frmlev2_%x2%,res
    ChromeGetElementData>session_id,frmlev2_%x1%,text,frm2_str
    MessageModal>frm2_str
  EndWhile
EndWhile
But it just returns blank strings

Using javascript below it retrieves the frames on the same way and get usefull strings in return:

Code: Select all

document.getElementsByTagName("frame")[0].outerHTML
document.getElementsByTagName("frame")[0].attributes.cd_frame_id_
document.getElementsByTagName("frame")[0].attributes.name
Please assist me?
Last edited by ocnuybear on Fri Sep 16, 2022 10:45 am, edited 1 time in total.

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Cannot access frame attributes

Post by Grovkillen » Thu Sep 15, 2022 7:43 pm

Sure it's a string you get when using the Javascript (in console?). Maybe you need to use stringify to get it back into MS? My guess is that the values are objects and not strings.
Let>ME=%Script%

Running: 15.0.24
version history

ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

Re: Cannot access frame attributes

Post by ocnuybear » Fri Sep 16, 2022 3:56 am

The javascript used in the console was only to verify what is going on. I'm not using it in MS, but sure the javascript maybe returning objects instead of strings. What I do not understand when switching with

Code: Select all

ChromeSwitchFrame>session_id,element,frmlev1_1,res
the first loop loop twice and the second one 3 times and res=8 and not 0 meaning it did not switch the frame succesfully, but it did, I'm confused how this is working now?
Last edited by ocnuybear on Fri Sep 16, 2022 10:58 am, edited 1 time in total.

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Cannot access frame attributes

Post by hagchr » Fri Sep 16, 2022 10:18 am

Hi, when I ran the script I get error code 10 for some SwitchFrames, not sure what it means. One important thing seems to be the context you are in and what is visible. For my own testing I re-wrote the script and re-started from the main page when looking for each frame, see script below. I am sure it can be done much shorter/better but I include it for illustration. One thing that can possibly be used is, in the reference it says:
To return the context to the parent window leave method empty or use index with reference set to null.
Then maybe you can handle the context without starting from scratch in each iteration.

Code: Select all

Let>CHROMEDRIVER_EXE=D:\Downloads\chromedriver.exe
ChromeStart>session_id

Let>urlTarget=http://the-internet.herokuapp.com/nested_frames

//Find frames on main page (frmlev1_count stores number of level 1 frames) 
ChromeNavigate>session_id,url,urlTarget
ChromeFindElements>session_id,tag name,frame,frmlev1

Let>i=0
While>i<frmlev1_count
  Add>i,1

  // Navigate from top to level 1 frame i
  ChromeNavigate>session_id,url,urlTarget
  ChromeFindElements>session_id,tag name,frame,frmlev1
  ChromeSwitchFrame>session_id,element,frmlev1_%i%,res
  
  // Find level 2 frames in level 1 frame i
  ChromeFindElements>session_id,tag name,frame,frmlev2
  
  // If level 2 frame i contains frames then loop over them
  If>frmlev2_count>0
    Let>j=0
    While>j<frmlev2_count
      Add>j,1
            
      // Navigate from top to level 2 frame j
      ChromeNavigate>session_id,url,urlTarget

      // Find overall frames
      ChromeFindElements>session_id,tag name,frame,frmlev1

      // Switch to level 1 frame i, ie Top or Bottom, 1-2
      ChromeSwitchFrame>session_id,element,frmlev1_%i%,res
      ChromeFindElements>session_id,tag name,frame,frmlev2

      // Switch to level 2 frame j, ie Left, Middle, or Right, 1-3
      ChromeSwitchFrame>session_id,element,frmlev2_%j%,res
      ChromeFindElements>session_id,tag name,body,frmlev3
      ChromeGetElementData>session_id,frmlev3_1,text,strRes
      
      MDL>strRes
    EndWhile
  
  EndIf
  
  // If level 2 frame i does not contain any frames, then get body text  
  If>frmlev2_count=0
    ChromeFindElements>session_id,tag name,body,frmlev2
    ChromeGetElementData>session_id,frmlev2_1,text,strRes
    MDL>strRes
  EndIf
 
EndWhile

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

Re: Cannot access frame attributes

Post by Dorian (MJT support) » Fri Sep 16, 2022 10:38 am

Very nice solution there.

We've just been going through the original post and one of the issues we identified is that the 2nd loop doesn't switch context back to the original frame, hence the error when trying to switch frame again. We always have to switch back to the parent.

Code: Select all

//Switch context back to parent
ChromeSwitchFrame>session_id,index,null,res
And then switch to the next frame.

So for example you can't just to this...

Code: Select all

ChromeSwitchFrame>session_id,index,0,res
ChromeSwitchFrame>session_id,index,1,res
ChromeSwitchFrame>session_id,index,2,res
You have to do this :

Code: Select all

ChromeSwitchFrame>session_id,index,0,res
ChromeSwitchFrame>session_id,index,null,res
ChromeSwitchFrame>session_id,index,1,res
ChromeSwitchFrame>session_id,index,null,res
ChromeSwitchFrame>session_id,index,2,res
ChromeSwitchFrame>session_id,index,null,res
Once we were looking at the correct frame, we were able to get the attributes.

For example :

Code: Select all

ChromeGetElementData>session_id,frmlev1_%x1%,attribute/outerHTML,frm1_str
Returned : FRM1_STR=<frame src="/frame_top" scrolling="no" name="frame-top">

And

Code: Select all

ChromeGetElementData>session_id,frmlev1_%x1%,attribute/name,frm2_str
Returned : FRM2_STR=frame-top
Yes, we have a Custom Scripting Service. Message me or go here

ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

Re: Cannot access frame attributes

Post by ocnuybear » Fri Sep 16, 2022 10:45 am

Thank you guys :D :D :D

I understand navigating through the frames much better now and as many websites nowadays have nested frames, people can use this code to find that stubborn "Hard to access Elements" within nested frames easier !

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