In this Python Selenium tutorial we want to learn about Handling Frames with Python Selenium, so Frames are also known as iframes, they are HTML elements that allows you embedding one web page inside another page. when working with browser automation using Python Selenium, it is important to understand how to handle frames to interact with elements inside them. in this tutorial we are going to talk about different techniques that we can use.
For working with the example of this tutorial you need some requirements, first you should have installed Python in your system, then we need to install Python Selenium and you can use pip for that like this.
1 |
pip install selenium |
Note: You can download the drivers from here.
Let’s talk about different ways to handle frames.
Selenium Switching to a Frame by Index
If a web page contains multiple frames, you can switch to a specific frame using its index. The index starts from 0, and it indicates the first frame on the page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Open a web page with frames driver.get("https://example.com") # Switch to the first frame (index 0) driver.switch_to.frame(0) # Perform actions within the frame # ... # Switch back to the main content driver.switch_to.default_content() # Close the browser driver.close() |
Selenium Switching to a Frame by Name or ID
Frames can also be identified by their name or ID attribute. You can switch to a frame by specifying its name or ID.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Open web page with frames driver.get("https://example.com") # Switch to frame by name driver.switch_to.frame("frame_name") # Perform actions within the frame # ... # Switch back to the main content driver.switch_to.default_content() # Close the browser driver.close() |
Selenium Switching to a Frame by WebElement
If you have a reference to a WebElement that represents a frame, you can switch to that frame directly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Open web page with frames driver.get("https://example.com") # Locate frame element frame_element = driver.find_element_by_id("frame_id") # Switch to frame element driver.switch_to.frame(frame_element) # Perform actions within the frame # ... # Switch back to the main content driver.switch_to.default_content() # Close the browser driver.close() |
Python Selenium Nested Frames
If a frame is nested inside another frame, you need to switch between frames sequentially to reach the desired frame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Open web page with nested frames driver.get("https://example.com") # Switch to the first frame (outer frame) driver.switch_to.frame("outer_frame") # Switch to the second frame (inner frame) driver.switch_to.frame("inner_frame") # Perform actions within the inner frame # ... # Switch back to the main content driver.switch_to.default_content() # Close the browser driver.close() |
Learn More on Python Selenium
- Web Automation with Python Selenium
- Page Navigation with Python Selenium
- Python Selenium and CSS Selectors
- Python Selenium and XPath Usage
- Working with Python Selenium Cookies
- Implicit and Explicit Waits in Python Selenium
- Handling Popups with Python Selenium
- How to Capture Screenshots with Python Selenium