Welcome to what I name “body hell”. I had a really comparable scenario.
- Doc
- High body
- navigation body
- content material body
- High body
The very first thing to notice is selenium will at all times help you return as much as the doc stage (aka defaultContent
) no matter what body you are at the moment in. Shifting between frames is like transferring between directories with out the likelihood to leap on to a body.
Instance: In the event you’re at the moment on the outermost doc stage, you possibly can’t simply bounce into content material body
, you need to swap to High
, then swap to content material body
.
The second downside I bumped into is loading states of content material and frames. Clicking a button within the navigation body or content material body would possibly trigger the WHOLE web page to reload or maybe simply the content material body.
I needed to reset to the highest and look forward to every body stage to load earlier than going into the subsequent stage. Right here is an instance code of switching to content material body
no matter the place you might be at the moment:
def switch_to_frame_content(self, wait: WebDriverWait):
"""strikes to the content material body"""
self.driver.switch_to.default_content()
wait.till(ec.frame_to_be_available_and_switch_to_it(locators.top_frame))
wait.till(ec.frame_to_be_available_and_switch_to_it(locators.content_frame))
return
Here’s a use case the place clicking a button within the content material body brought about the content material body itself to be reloaded throughout the High body.
self.click on(confirm_button)
self.switch_to_frame_content(wait)
wait.till(ec.element_to_be_clickable(next_button_to_click))