{"id":14693,"date":"2023-11-07T13:17:56","date_gmt":"2023-11-07T07:47:56","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=14693"},"modified":"2025-10-10T09:14:40","modified_gmt":"2025-10-10T13:14:40","slug":"iframes-in-selenium","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/iframes-in-selenium\/","title":{"rendered":"IFrames in Selenium"},"content":{"rendered":"\n<p>Iframes in selenium web driver will be the web page or an inline frame which is embedded in another web page or may be an HTML document that is embedded inside the HTML document. The Iframe will be often used to add the content from the sources like an advertisement into a web page. This iframe will be defined as &lt;iframe> tag<\/p>\n\n\n\n<p>We can identify the iframe by just seeing the web page or may be by inspecting Firebug.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Oda9V0ZT33Y_NTHnxQNU9zq0g6oLrPZnhnwyf75fFb3pPgumeFAPSUm4XEm7OeA1NFKwUbipQg02pFR2FnTWTBcaBOVmtZhZarKaZYXfLMZDCzHoYb7UJQviXWvCo8QiGFXhCsTntCfxRmynCumqPw\" alt=\"How to identify the iframe using Selenium WebDriver\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>We can identify he frames in the selenium by using the methods by:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>By right clicking on the element, if we find an option like \u2018This Frame\u2019 then it is iframe<\/li>\n\n\n\n<li>We should right click on the page and look into \u2018view page source\u2019 and search with \u2018iframe\u2019 as we can find any tag name with a \u2018iframe\u2019 then it will be the page consisting an iframe.<\/li>\n<\/ul>\n\n\n\n<p>This diagram views that \u2018This Frame\u2019 option is available. It is not sure that it its an iframe.&nbsp;<\/p>\n\n\n\n<p>We can recognise the total number of iframes by using the following syntax<\/p>\n\n\n\n<p>int size = driver.findElements (By.tagName (&#8220;iframe&#8221;)).size ();<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How we have to switch the elements in IFrames in Selenium by using the webDriver commands?<\/strong><\/h2>\n\n\n\n<p>We can shift the elements and manage frames in selenium using 3 modes.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>By Index<\/li>\n\n\n\n<li>By Name or Id<\/li>\n\n\n\n<li>By Web Element<\/li>\n<\/ul>\n\n\n\n<p>We can switch to frame by index<\/p>\n\n\n\n<p>Index is the property for frame handling in Selenium as it is switch to it.<\/p>\n\n\n\n<p>Index of the iframe starts with \u20180\u2019<\/p>\n\n\n\n<p>Suppose if there are 100 frames in page,we can switch to the frame in selenium by using the index.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>driver.switchTo().frame(0);<\/li>\n\n\n\n<li>driver.switchTo().frame(1);<\/li>\n<\/ul>\n\n\n\n<p>We can switch to the frame by Name or ID<\/p>\n\n\n\n<p>Name and ID are the attributes for handling frames in the selenium through which we can switch to the iframe.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>driver.switchTo().frame(\u201ciframe1\u201d);<\/li>\n\n\n\n<li>driver.switchTo().frame(\u201cid of the element\u201d);<\/li>\n<\/ul>\n\n\n\n<p>This is not possible to click iframe directly by XPath as this iframe as we can use frame and then click using then XPath.<\/p>\n\n\n\n<p>First we have to initialize the Firefox driver.<\/p>\n\n\n\n<p>Navigate to the required site which consists the iframe.<\/p>\n\n\n\n<p>Maximized window<\/p>\n\n\n\n<p>driver. SwitchTo ().frame(&#8220;a077aa5e&#8221;);<\/p>\n\n\n\n<p>This will have to find out the id of the iframe by verifying through Firebug.<\/p>\n\n\n\n<p>Then we have to switch to the iframe through the ID.<\/p>\n\n\n\n<p>driver.findElement (By.xpath(\u201chtml\/body\/a\/img&#8221;)).click();<\/p>\n\n\n\n<p>This is to know out the xpath of the element that need to be selected. Then click the element by using the web driver command. For example<\/p>\n\n\n\n<p>public class SwitchToFrame_ID {<\/p>\n\n\n\n<p>public static void main(String[] args) {<\/p>\n\n\n\n<p>WebDriver driver = new Firefox Driver (); \/\/navigates to the Browser<\/p>\n\n\n\n<p>&nbsp; &nbsp; driver.get(&#8220;http:\/\/demo.iitforce.com\/test\/iitforcehome\/&#8221;);&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;\/\/ goes to the page consisting an iframe<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; driver.manage().window().maximize();<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; driver.switchTo().frame(&#8220;a077aa5e&#8221;); \/\/switching the frame by ID<\/p>\n\n\n\n<p>System.out.println(&#8220;********We are switch to the iframe*******&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; driver.findElement(By.xpath(&#8220;html\/body\/a\/img&#8221;)).click();<\/p>\n\n\n\n<p>&nbsp;&nbsp; &nbsp; &nbsp; \/\/Clicks the iframe<\/p>\n\n\n\n<p>&nbsp;&nbsp; System.out.println(&#8220;*********We are done***************&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>This output is it transfers to the page consisting the above iframe and clicks on the iframe.<\/p>\n\n\n\n<p>Switch to the frame by Web element<\/p>\n\n\n\n<p>We can shift to the iframe by using web element<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>driver.switchTo().frame(WebElement);<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How we can switch to the main frame back?<\/strong><\/h2>\n\n\n\n<p>We should come out of the iframe. To move we can either use the switchTo ().parent Frame () or if we want to get back to the main frame, we can use switchTo ().default Content ().<\/p>\n\n\n\n<p>Driver. SwitchTo ().parent Frame ();<\/p>\n\n\n\n<p>&nbsp;&nbsp;Driver. SwitchTo ().default Content ();<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How we can use the Frame where we cannot use Switch using ID or may be Web Element?<\/strong><\/h2>\n\n\n\n<p>Suppose if there are 100 frames in the page and there is no ID we have we can just don\u2019t know from which frame it is required element which is being loaded. The solution for the concern we can know the index of the iframe through which the element will be loaded and also switch to the iframe through the index.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Questions<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>What is Iframe?<\/li>\n\n\n\n<li>How to switch Iframes between elements in selenium web driver?<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Iframes in selenium web driver will be the web page or an inline frame which is embedded in another web page or may be an HTML document that is embedded inside the HTML document. The Iframe will be often used to add the content from the sources like an advertisement into a web page. This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":14695,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-14693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/14693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=14693"}],"version-history":[{"count":1,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/14693\/revisions"}],"predecessor-version":[{"id":30556,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/14693\/revisions\/30556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/14695"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=14693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=14693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=14693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}