I'm trying to select an element that resides within an iframe and may reside within other iframes.
Is it possible to select an element within some (child) iframe in (python) selenium without selecting the iframe first? Is there a way to somehow "loop" through each iframe and check where my element can be found...?
How to do this in case the element, html content and iframe may have just been loaded...?
It should be easy to write your own recursive finder. Sorry, I don't know python, but in Java it would look like this:
No, cannot pass Selenium, no need to switch to the corresponding
iframe
.reason:
When loading a page, Selenium's focus remains on the top window by default. Top window contains other
<iframes>
and framesets. Therefore, when we need to interact with a WebElement inside an iframe, we must switch to the corresponding<iframe>
via one of the following methods:Frame switching method:
We can switch to the frame in 3 ways.
By frame name:
Name The attribute of iframe through which we can switch to it.
Example:
By frame ID:
TheID attribute of iframe, through which we can switch to it.
Example:
Index by frame:
Assuming that the page has 10 frames, we can switch to iframe through index.
Example:
Switch back to host:
We can use
default_content()
orparent_frame()
to switch back to the main frameExample:
Better way to switch frames:
A better way to switch frames is to induce
## To get the availability of the expected framework as follows:
WebDriverWait# by setting
expected_conditionsto
frame_to_be_available_and_switch_to_itFrame ID:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"id_of_iframe"))Frame name:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"name_of_iframe")))FrameworkXpath:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"xpath_of_iframe")))Frame CSS:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"css_of_iframe")))refer to