This article serves as a reference for training test steps that interact with elements in iframes. Iframes, also known as inline frames, embed content from another website in an application, such as videos and maps.
Recording steps in iframes
When you record steps that interact with elements in an iframe, the mabl Trainer automatically detects the iframe and adds a switch context step before the recorded steps. If you interact with the rest of the page later, the Trainer adds another switch context step to switch back to the root frame. You can adjust how an existing switch context step identifies its iframe with Configure Find, but you cannot manually create a switch context step using a custom XPath, CSS selector, or JavaScript snippet.
Training steps in an iframe and switching back to the root frame
Nested iframes
If you are testing an application with nested iframes, the Trainer automatically adds a switch context step for each iframe you interact with.
Iframes with dynamic names
If the attributes of an iframe, such as its name or URL, change dynamically, use Configure Find to identify a fixed attribute by which to identify the iframe: … (More actions) > Configure Find.
Choose a stable and unique attribute to identify the iframe. For example, if an iframe’s URL changes every time a test runs, but its name is always “childOne”, update the switch context step to pick the iframe with the name “childOne”:
Using Configure Find for iframes with dynamic attributes
If only part of the iframe attribute is stable across runs, use a contains match. For example, if an iframe’s name is form-frame-1234 and the trailing digits change every run, configure the switch context step to match iframes where name contains form-frame. You can match on name, src, title, id, or other attributes. Use whichever is most stable for your application.
Using XPath and CSS selectors in iframes
With custom find steps, the Trainer only searches the current frame. If you want to add custom find steps to target an element that exists within an iframe using XPath or CSS, make sure the Trainer is focused on the correct frame:
- Record a click step in the iframe to create a switch context step. By switching into the iframe, the mabl Trainer will be focused in the correct frame for creating the custom find step.
- If the test already has a switch context step, replay it before adding the custom find step to make sure the Trainer is focused on the correct frame.
Troubleshooting: “Switch context to unknown element”
If a switch context step records as switch context to unknown element, the Trainer couldn’t capture the iframe’s attributes when you clicked. The most common cause is the Disable iframe tracking Trainer preference. To check:
- In the mabl Trainer, open Settings, in the bottom-right of the Trainer window above the Save button.
- If Disable iframe tracking is checked, uncheck it.
- Re-record the click that should switch into the iframe.
If unchecking Disable iframe tracking doesn’t resolve the issue, try:
- Updating to the latest desktop app version: Help > Check for updates.
- Clearing the Trainer’s session cache - Help > Troubleshooting > Clear session cache/storage - and re-recording.
- Confirming the
iframeelement is inside an actual tag.