In Previous section, we have discussed about how to handle Alert Popup box in Selenium Python. Now we will concentrate on Frame in Selenium Python. According to W3C standard frame is not a good option to have inside a particular website.
- What is Frame?
- Handling Iframe in Selenium
- Handling Dynamic Frames in Selenium
What is Frame?
Before understanding the handing Frame in Selenium Python, we need to check what Iframe is. Iframe is a HTML page embedded inside another HTML . Iframe is used for displaying the advertisement within a page from external source.Iframe is explicitly defined on HTML document using tag named <iframe>.
You can ask, how you will get to know that a web page has iframe or not?
The simple answer is from page source. You need to click right click on the web page and go to source page of the website and search for <iframe> tag. If that page source contains <iframe> tag then the web page has the iframe. See the below screenshot to know about iframe.
From the automation testing prospective, how we will handle the iframe if this is also a web page inside web page. Let’s understand the below code.
Frame_list = driver.find_elements(By.TAG_NAME(“iframe”))
In the above line, find_elements() will search for the tag name with iframe and store in iframe variable which is a List type object. In this approach, this may return more than one iframe tag element. Once we stored all the iframe tag element in List type object “iframe”, we can traversed the list and operate based on the requirement.
If you want to return only one iframe element, then you need to use find_elements(). See the below line.
Frame_list = driver.find_element(By.TAG_NAME(“iframe”))
Frames cannot be seen directly in a web page. Frame is also kind of web element. Whenever you design any page there are some element which is not visible directly. Inside Frame you can have other element also like Text field, dropdown etc. If you want to locate the element inside the frame then you may not get through our traditional approach like find_element. This is because our driver is now in webpage not in frame.
To handle this we need to switch to our driver from the webpage to Frame. After completion, through default_content you can come back to the webpage and continue your work.
Handling Iframe in Selenium:
In previous section, you understood the basic concept of handling iframe in selenium. Now let’s discuss briefly.
Selenium provides some in-built methods to handle the iframe. Like alert popup box, you need to use switch_to() method to navigate to the iframe. Below are some methods which required handling the iframe.
- switch_to.frame (frameName)
- Frame (WebElement frame Element)
The frame index starts from 0. Second frame has the index 1and so on.
When the mentioned frame number is not found, at that time, this will throw NoSuchFrameException. We can use the id of the frame once it is identified. See below code.
driver. switch_to.frame(2) //Here index of the frame is 2
This method will allow script to find the Frame which has the frame name mentioned with <iframe> tag.
The frame name is actually developer defined.
Frame name should be enclosed with double quote (‘ ’) to be consider as string.
When the mentioned iframe name is not found, at that time, this will throw NoSuchFrameException. See the below code.
Frame (WebElement frame Element):
This method allows user to find the iframe by using the web element. You can find the web element by using any of the location strategy like ID, Class Name, Name, LinkText, Partial Link Text etc.
When you are trying to find the frame which is not present in the web page then this method will throw NoSuchFrameException. Same like this it you are trying to find the frame which is not active in the current page then it will throw StaleElementReferenceException.
frameElement = driver.find_element(By.ID(“ae82pms0”)); driver.switch_to.frame(frameElement);
This method is used to navigating the iframe from iframe to main page. Below is the syntax to achieve this.
There is an another method which help to navigate to iframe is driver.switch_to().parent_frame (). The main difference between default_content and parent_frame is that the first method switches the control to the main web page regardless of the number of frames within the web page, while the second method switches the control to the parent frame of the current frame.
from selenium import webdriver from selenium.webdriver.common.by import By from webdriver_manager.chrome import ChromeDriverManager import time driver = webdriver.Chrome(ChromeDriverManager().install()) driver.get('http://demo.automationtesting.in/Frames.html') driver.maximize_window() frame_element = driver.find_element(By.NAME, 'SingleFrame') driver.switch_to.frame(frame_element) frame_ele = driver.find_element(By.CSSSelector, 'div.col-xs-6') frame_ele.send_keys('Welcome') driver.switch_to.default_content()
Handling Dynamic Frames in Selenium:
From the above section, you noticed that we can find the iframe in web page by using multiple strategy like ID, Frame Name, Frame Element, index, Default Content etc. But one disadvantage of these methods is you will not get the actual element if the value of the Id or name is changing every time.
<iframe id = ‘frame_12>…</iframe>
In this example number value of frame id varies and frame_ is constant with page load.
What will be your approach for navigating to the iFrame.
Yes . We can identify the above frame uniqu by using the below XPath.
- Iframe is used for displaying the advertisement within a page from external source.Iframe is explicitly defined on HTML document using tag named <iframe>.
- Selenium provides some in-built methods to handle the iframe. Like alert popup box, you need to use switch_to() method to navigate to the iframe. Below are some methods which required handling the iframe.
Now you are clear about the Frame in Selenium Python and how to automate to handle scenario like Frames. In Next section, we will understand more about the advance popup handling such as Authentication Popup.