How to use ‘Chrome DevTools’ to find iframe locator(s):
- Use Chrome to navigate to this iframe example: <iframe>: The Inline Frame element - HTML: HyperText Markup Language | MDN
- Press F12 to open ‘Chrome DevTools’.
- Click on the ‘Console’ tab and input
document.querySelectorAll("iframe")
, press Enter. - Expand the Nodelist(2).
- Click on the second iframe result, the iframe is loaded.
- Right-click on the result, click ‘Copy’ > ‘Copy XPath’.
- Press ‘Ctrl+F’ and paste the result into the search field, press Enter.
Result: //*[@id="frame_a_simple_iframe"]
is found on the page.
If more than one iframe result were to display you could use (//*[@id="frame_a_simple_iframe"])[1] or (//*[@id="frame_a_simple_iframe"])[2]
and so on to pick the target iframe. Using (//iframe)[1] or (//iframe)[2]
would also work. In this case (//iframe)[2]
would be the locator to use. You could use (//iframe)
to cycle through all iframes on the page.