Switch To Default ContentPage


Switch to iframe by using switch_to.frame() method and switch back to default content page using switch_to.default_content().


Example:

  • Launch the webpage "http://www.dummypoint.com/Frame.html"
  • Switch to iframe by using switch_to.frame() method by passing webelement object.
  • After switching get the text from iframe using ID locator.
  • Now switch back to default content page and get the text from it.
  • Wait for 5 sec.
  • Close the Web Browser


Switch_To_Default_ContentPage_using_selenium_python.py

from selenium.webdriver.common.by import By
from selenium import webdriver
import time

driver = webdriver.Chrome("/Users/admin/Documents/Others/Drivers/chromedriver.exe")  # Using Chrome Driver

driver.get("http://www.dummypoint.com/Frame.html")
assert "Selenium Template" in driver.title

# Switch to iframe by WebElement
time.sleep(2)
ele = driver.find_element(By.ID,"f2")
driver.switch_to.frame(ele)

# After switching get the text from iframe using ID locator
data = driver.find_element(By.ID,"framename")
print("Frame Name is : ",data.text)


# Switching back to normal content page from frame
time.sleep(2)
driver.switch_to.default_content()

# Get the text from webpage content page after switching back.
data = driver.find_element(By.ID,"framename")
print("content in the WebPage is : ",data.text)

time.sleep(5)
driver.quit()