Open In App

How to handle Frames/iFrames in Selenium with Python

Last Updated : 08 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we can be running with Python.

HTML outlines are utilized to isolate your program window into numerous segments where each part can stack a different HTML report. An assortment of edges in the program window is known as a frame set. The window is partitioned into outlines likewise the tables are composed: into lines and segments.

Requirement: You need to install chromedriver and set path. Click here to download.for more information follows this link.

 Handle Frames/iFrames:-

switch_to.frame(name)

Process:

This web page is divided into three frames, left top (1st frame) and left bottom(2nd frame) and the third frame. All the frames interconnected. Then we perform these actions by selenium:

  • First of all, switch to the default frame to the first frame.
  • Then find the element using link text method
  • Go back to the default frame.
  • Then go to the 2nd frame
  • Find element using the link text method
  • Go back to the default frame
  • Then switch to the 3rd frame.
  • Then find element by x path.

Implementation:

Python3




# importing the modules
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
 
# using chrome driver
driver = webdriver.Chrome()
 
# web page url
 
# switch to 1st frame
driver.switch_to.frame("packageListFrame")
 
# click on 1st frame
driver.find_element_by_link_text("org.openqa.selenium.opera").click()
 
# back to default web page frame
driver.switch_to.default_content()
 
# switch to 2nd frame
driver.switch_to.frame("packageFrame")
 
# click on  2nd frame
driver.find_element_by_link_text("OperaOptions").click()
 
# back to default web page frame
driver.switch_to.default_content()
 
# switch to 3rd frame
driver.switch_to.frame("classFrame")
 
# click on  2nd frame
driver.find_element_by_xpath('/html/body/div[1]/ul/li[4]/a').click()
time.sleep(4)


Output:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads