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.
//*[@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"]) or (//*[@id="frame_a_simple_iframe"]) and so on to pick the target iframe. Using
(//iframe) or (//iframe) would also work. In this case
(//iframe) would be the locator to use. You could use
(//iframe) to cycle through all iframes on the page.