I am trying to select an element which resides inside an iframe and probably in other iframes.
Is it somehow possible to select an element within some (sub-)iframe in (python)selenium without selecting the iframes before? Is there a way to 'loop' over every iframe somehow and to check where to find my element...?
And how to do that in the case elements and html stuff and iframes might just about being loaded...?
Writing your own recursive finder should be easy enough. Apologies, don't know python but in Java it would be something like:
No, it is not possible to interact with any
WebElement
within aniframe
throughSelenium
without switching to the respectiveiframe
.Reason :
When a page is loaded,
Selenium
's focus by default remains on theTop Window
. TheTop Window
contains the otheriframes
and theframesets
. So when we need to interact with aWebElement
which is within an iframe we have to switch to the respectiveiframe
through one of the below-mentioned methods :Frame Switching Methods :
We can switch over to frames by 3 ways.
By Frame Name :
Name
attribute of iframe through which we can switch to it.Example:
By Frame ID :
ID
attribute of iframe through which we can switch to it.Example:
By Frame Index :
Suppose if there are 10 frames in the page, we can switch to the iframe by using the index.
Example:
Switching back to the Main Frame :
We can switch back to the main frame by using
default_content()
orparent_frame()
Example:
A Better Approach to Switch Frames:
A better way to switch frames will be to induce
WebDriverWait
for the availability of the intended frame withexpected_conditions
set toframe_to_be_available_and_switch_to_it
as follows :Through
Frame ID
:Through
Frame Name
:Through
Frame Xpath
:Through
Frame CSS
: