Handling iFrames in Selenium Webdriver
Posted By : Neha Saini | 31-Dec-2018
What is an iframe?
iframe></iframe> tag is used to define iFrame in HTML.
The iFrames are used to insert content from another source. Eg: insert an advertisement into a web page.
How to identify the iframe:-
1. Right click on the web element, if we find “This Frame” in the options then it is an iframe.
2. Right click on the web page and click “View Page Source” and then search “iframe” if find any tag name with iframe then it is “iframe”.
Int size = driver.findElements(By.tagName("iframe")).size()
The most effective method to switch over the components in iframes utilizing Web Driver directions:-
By using 3 ways we can switch over the element in the frame:
• By Index
• By Name or Id
• By Web Element
1. Switch to the frame by index:-
The index is an attribute for the Iframe and by using index we can switch to the Iframe.
Index starts with ‘0’.
Suppose if there are 80 frames on the page, we can use the index to switch on the Iframe.
driver.switchTo().frame(0);
driver.switchTo().frame(1);
2. Switch to the frame by ID or Name: -
ID or Name are the attributes of Iframe and by using these attributes we can switch to the iframe .
driver.switchTo().frame("iframe1"
driver.switchTo().frame("id of the element")
3. Switch to the frame by Web Element:-
driver.switchTo().frame(WebElement);
By using Web Element we can switch to the iframe .
How to switch back to the Main Frame:-
To switch back to the parent frame, we can use
switchTo().parentFrame() .
Or if we want to switch to the main frame(or most parent frame), we can use
switchTo().defaultContent()
driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
Request for Proposal
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 Saini
Neha is a QA Engineer in Oodles and have good knowledge of Manual Testing and Automation Testing , always eager to learn new things.