Switch frame in selenium python

 What is iframe?

Full form of iframe- inline frame

The inline frames allow you to display a second, separate webpage within your main webpage.

(or)

An inline frame is used to embed another html document within the current HTML document.

When iframe is used in web application or web page ?

Iframe is used to include a third-party widget or content.

For example: Facebook like button, a YouTube video, or an advertising section on your website.

As the seperate secondary webapge is embedded in to the main web page. It is necessary to move the secondary webapage(i.e. iframe) to find the element in it.

How we can move/switch to the secondary webpage or iframe ?

Selenium WebDriver provides three ways to switch over the elements in an iframe.

  • By Index
  • By Name or Id
  • By Web Element

switch_to_frame(index) (or) switch_to.frame(index)

This method is used to identify a frame with the help of frame id and then switch the focus to that particular frame.

Syntax −

driver.switch_to_frame(numeric value), where frameid/name is the id/name attribute present under the frame/iframe tag in HTML.

switch_to_frame(id/name) (or) switch_to.frame(id/name)

This method is used to identify a frame with the help of frame id or name and then switch the focus to that particular frame.

Syntax −

driver.switch_to_frame("frameid/name"), where frameid/name is the id attribute present under the frame/iframe tag in HTML.

switch_to_frame(webelement) (or) switch_to.frame(webelement)

This method is used to identify a frame with the help of frame webelement and then switch the focus to that particular frame.

Syntax −

driver.switch_to_frame(webelement), where frameclassname is the name of class attribute present under the frame/iframe tag in HTML. 

switch_to_parent_frame() (or) switch_to.parent_frame()

This method is used to come out of the present frame, then we can access the elements outside that frame and not inside of that frame. 


switch_to_default_content() 
(or) switch_to.default_content()

This method is used to come out of all the frames and switch the focus at the page. Once we move out, it loses the access to the elements inside the frames in the page.

from selenium import webdriver

driver = webdriver.Chrome(r'F:\chromedriver.exe')
driver.implicitly_wait(30)
driver.get("https://www.globalsqa.com/demo-site/frames-and-windows/#iFrame")

#switch to frame using web element
frame_element = driver.find_element_by_css_selector('iframe[name*="global"]')
driver.switch_to.frame(frame_element)

find_text = driver.find_element_by_css_selector('a[title="Trainings"]').text
print(find_text)
driver.switch_to.default_content()

#switch to frame using id or name
driver.switch_to.frame('globalSqa')
find_text = driver.find_element_by_css_selector('a[title="Trainings"]').text
print(find_text)
  • ----------------------------------Another way to switch frame ----------------------------------

We can also switch to frame using WebDriverWait inconjunction with expected_conditions as frame_to_be_available_and_switch_to_it()
 
Syntax
WebDriverWait(driver, timeout).until(EC.frame_to_be_available_and_switch_to_it((locators))) 
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(r'F:\chromedriver.exe')
driver.get("https://www.globalsqa.com/demo-site/frames-and-windows/#iFrame")
try:
# Switch to frame using below method
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "globalSqa")))
find_text = driver.find_element_by_css_selector('a[title="Trainings"]').text
print(find_text)
finally:
driver.quit()
What happens when finding element directly while there is frame with out switching to it ?

At that instance its unable to find that element and it throws error as No such element exception


Comments

Post a Comment

Popular posts from this blog

Python property method

Learn basics of python collection data type (list, tuple, dictionary and set) instantly