How To Handle IFrames Using Selenium WebDriver
Posted By : Neha Dahiya | 25-May-2018
What is iFrame?
iFrame is nothing but an HTML document which is embedded in another HTML document. In HTML, it is defined as <iframe></iframe> tag. Firstly, we have to switch to the frame and then we can click using XPath. It is impossible to directly locate an iFrame on a webpage through XPath as it an iFrame so it is very important to understand to use iframes we have to switch between different iFrames present on a page. To switch between different iFrames “
1. Switch to the frame by Name:
Id and name are attributes used to switch between the frames.
Have a look at the iFrame HTML code which has the name attribute value “iframe1”. You can switch to the iFrame using command driver.switchTo().frame(“iframe1”). You can use the below code to switch iFrame by Name :
2. Switch to the frame by ID:
There is also an ID attribute similar to the name one. You can switch to the iFrame using command driver.SwitchTo().frame(“IF1”). Use the following code to switch iFrame by ID :
3. Switch to the frame by WebElement:
We can also switch through iFrame by passing iFrame WebElement to the driver.switchTo().frame() command. You will have to find the element which can be done using any locatorand then passing it to command. Below code can be used to switch iFrame by WebElement :
Switching back to
To perform any other operation outside the iFrame, it is very important to go back to the main page. A
Thanks!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Neha Dahiya
Neha is a bright QA Engineer with skills in manual testing . Apart from finding bugs in application, she loves sketching and painting.