Selenium Python – Selecting and focusing on the correct frame or iframe

Had an issue today where a modal opened up on the site, but I was not able to click anything inside it. After digging through the source realized that it was in an iframe. It was the only iframe in the page so the xpath element to find it was simple, just “//iframe” . To focus on this iframe used code like this:

frame_modal = self.driver.find_element_by_xpath("//iframe")
        self.driver.switch_to_frame(frame_modal)

Following some actions inside the iframe, I had to now return to the main page. This is what did it:

self.driver.switch_to_default_content()

I was now back on my main page able to continue the test. Took me a while to figure this out so posting here as it might help someone else.