(WebAutomation)How to select Elements of iframes in Robin UI Spy?

In Robin UI Spy I am unable to get control from iFrame please suggest.

@rushi.dalvi does the iframe have a ‘src’ attribute on the HTML code? Were you able to extract a selector through UISpy?

Yes its have src attribute…
test

please suggest…

Thanks.

@rushi.dalvi the iframes that contain src attribute are called cross-domain iframes.

In order to interact with elements inside cross-domain iframes the following approach must be used:

  • Use the “WebAutomation.GetDetailsOfElement” Action in order to get the source link attribute.

  • Use the “WebAutomation.GoToWebPage” Action to navigate to that source (or the “WebAutomation.CreateNewTab” to open the web page to another tab)

  • Use the desired “WebAutomation” Actions to interact with the iframe on the new URL you have navigated to

In case this approach doesn’t work (which is highly possible to happen in your case, as long as the scr attribute ends with ‘EmptyDocument.html’), then the Javascript approach is also feasible.

Please check the following implementations for your reference:

#First approach get the source link and navigate
#I have used the Selector text representation for portability purposes
#Check here for reference: https://forum.robin-language.org/t/making-dynamic-controls/168
WebAutomation.LaunchChrome \
Url: 'https://www.w3schools.com/howto/howto_js_element_iframe.asp'\
WindowState:WebAutomation.BrowserWindowState.Normal \
ClearCache:False ClearCookies:False BrowserInstance=> Browser
WebAutomation.GetDetailsOfElement BrowserInstance: Browser \
Control: 'domcontainer{<{<{<SelectorSeparator>}>}>}uia3:// #myFrame'\ 
AttributeName: 'src' AttributeValue=> SourceLink
WebAutomation.GoToWebPage BrowserInstance: Browser Url: SourceLink
WebAutomation.PressButton BrowserInstance: Browser \
Control: 'domcontainer{<{<{<SelectorSeparator>}>}>}uia3:// a:contains("LEARN JAVASCRIPT")'



#Second approach with Javascript

WebAutomation.LaunchChrome \
Url: 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_checkbox' \
WindowState:WebAutomation.BrowserWindowState.Normal \
ClearCache:False ClearCookies:False BrowserInstance=> Browser
WebAutomation.ExecuteJavascript BrowserInstance: Browser \
Javascript:'''function ExecuteScript() { 
var iframe = document.querySelector('iframe[id="iframeResult"]');
var checkboxes = iframe.contentWindow.document.querySelectorAll('form input[type="checkbox"]');
checkboxes.forEach(function(element){
            element.checked = true;}); }''' \
Result=> Result

5 Likes

Kudos once again @jokoum, well done! What a useful script. Really, really helpful. :100:

This may just be due to my setup, but for the JS version I have to clear the Chrome cache in order to get reliably repeatable results, and a two-second delay seems to improve reliability further. My modded code is below.

WebAutomation.LaunchChrome      Url: 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_checkbox' \
                                WindowState:WebAutomation.BrowserWindowState.Normal \
                                ClearCache:True \
                                ClearCookies:False \
                                BrowserInstance=> Browser
wait 2

WebAutomation.ExecuteJavascript BrowserInstance: Browser \
                                Javascript:'''function ExecuteScript() { 
                                var iframe = document.querySelector('iframe[id="iframeResult"]');
                                var checkboxes = iframe.contentWindow.document.querySelectorAll(`form input[type="checkbox"]`);
                                checkboxes.forEach(function(element){
                                    element.checked = true;}); }''' \
                                Result=> Result 

EDIT: By the way, I’m dying to know how you built this control selector:

'domcontainer{<{<{<SelectorSeparator>}>}>}uia3:// #myFrame'

and this one:

'domcontainer{<{<{<SelectorSeparator>}>}>}uia3:// a:contains("LEARN JAVASCRIPT")'

Thanks in advance, and best regards,
burque505

thanks …its working…:star_struck::star_struck::grinning:

1 Like