IFrames by ID

Switch to iframe by using switch_to.frame() method and iframe id value.


Iframe by id

Example:

  • Launch the webpage "http://www.dummypoint.com/Frame.html"
  • Switch to iframe by using switch_to.frame() method by passing id value.
  • After switching get the text from iframe using ID locator.
  • Wait for 5 sec.
  • Close the Web Browser



IFrames_by_ID.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

ele = driver.find_elements(By.TAG_NAME, "iframe")

# To display number of Iframes in web page
print("List of iframe : ", len(ele))

# Switch to iframe by its id value
time.sleep(2)
driver.switch_to.frame("f4")

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


time.sleep(2)
driver.quit()