{"id":3471,"date":"2025-01-16T19:07:33","date_gmt":"2025-01-16T19:07:33","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3471"},"modified":"2025-01-17T10:48:22","modified_gmt":"2025-01-17T10:48:22","slug":"handling-frames-in-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/handling-frames-in-playwright-java\/","title":{"rendered":"Handling frames in Playwright Java"},"content":{"rendered":"<p><em>In this blog we will be utilizing Playwright Java to handle frames.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Fundamental overview of frames<\/li>\n<li>Handle frames<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>Fundamental overview of frames&nbsp;<\/strong><\/p>\n<p>A frame refers to an HTML page that is inserted into another page.&nbsp;<\/p>\n<p>Launch <a href=\"https:\/\/paytm.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/paytm.com\/<\/a> and hit \u2018Sign In\u2019 button<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3476\" width=\"478\" data-init-width=\"1198\" height=\"297\" data-init-height=\"744\" title=\"Screenshot 2025-01-17 at 12.38.11\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.38.11\u202fAM.png\" data-width=\"478\" data-height=\"297\" style=\"aspect-ratio: auto 1198 \/ 744;\"><\/span><\/p>\n<p>&nbsp; A distinct window pops up (as seen above) and this is a frame. The question arises as to how do we know if this popup is indeed a frame?&nbsp;<\/p>\n<p>By clicking the right mouse button on this window, the option to view the frame source becomes visible<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3477\" width=\"556\" data-init-width=\"1198\" height=\"398\" data-init-height=\"858\" title=\"Screenshot 2025-01-17 at 12.38.57\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.38.57\u202fAM.png\" data-width=\"556\" data-height=\"398\" style=\"aspect-ratio: auto 1198 \/ 858;\"><\/span><\/p>\n<p>Clicking outside the window will not show the &#8216;View frame source&#8217; option (see below). This means that this part of the html page is not a frame<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3478\" width=\"511\" data-init-width=\"1198\" height=\"297\" data-init-height=\"696\" title=\"Screenshot 2025-01-17 at 12.39.41\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.39.41\u202fAM.png\" data-width=\"511\" data-height=\"297\" style=\"aspect-ratio: auto 1198 \/ 696;\"><\/span><\/p>\n<p>&nbsp;This explanation provided a fundamental overview of frames.<\/p>\n<p><strong>Handle frames<\/strong><\/p>\n<p>We will now automate an action on an iframe. Navigate to<\/p>\n<p><a href=\"https:\/\/the-internet.herokuapp.com\/iframe\" style=\"outline: none;\" target=\"_blank\" rel=\"noopener\">https:\/\/the-internet.herokuapp.com\/iframe<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3479\" width=\"458\" data-init-width=\"1198\" height=\"291\" data-init-height=\"760\" title=\"Screenshot 2025-01-17 at 12.40.17\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.40.17\u202fAM.png\" data-width=\"458\" data-height=\"291\" style=\"aspect-ratio: auto 1198 \/ 760;\"><\/span><\/p>\n<p>&nbsp;Let&#8217;s examine the procedure for automating a test that will input text into the text area located in the frame above.<\/p>\n<p>The text area is represented by tag \u2018iframe\u2019 and has the \u2018id\u2019 <em>mce_0_ifr<\/em><\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3480\" width=\"579\" data-init-width=\"1198\" height=\"717\" data-init-height=\"1484\" title=\"Screenshot 2025-01-17 at 12.41.16\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.41.16\u202fAM.png\" data-width=\"579\" data-height=\"717\" style=\"aspect-ratio: auto 1198 \/ 1484;\"><\/span><\/p>\n<p>Since this frame is within a parent html page, the locator of parent html can than be written as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3481\" width=\"497\" data-init-width=\"1198\" height=\"395\" data-init-height=\"952\" title=\"Screenshot 2025-01-17 at 12.42.18\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.42.18\u202fAM.png\" data-width=\"497\" data-height=\"395\" style=\"aspect-ratio: auto 1198 \/ 952;\"><\/span><\/p>\n<p>This is how our code looks<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3482\" width=\"461\" data-init-width=\"1198\" height=\"252\" data-init-height=\"656\" title=\"Screenshot 2025-01-17 at 12.44.04\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.44.04\u202fAM.png\" data-width=\"461\" data-height=\"252\" style=\"aspect-ratio: auto 1198 \/ 656;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>The text appears in the text area of the frame as shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3483\" width=\"541\" data-init-width=\"1198\" height=\"296\" data-init-height=\"656\" title=\"Screenshot 2025-01-17 at 12.44.42\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.44.42\u202fAM.png\" data-width=\"541\" data-height=\"296\" style=\"aspect-ratio: auto 1198 \/ 656;\"><\/span><\/p>\n<p>&nbsp;Playwright does not require switching to a frame, unlike Selenium.&nbsp;<\/p>\n<p><strong>Source code<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Locator;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<\/p>\n<p><strong>public<\/strong><strong>class<\/strong> Blog27_iframes_PWJava_Example1 {<\/p>\n<p><strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>Browser browser = playwright.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>Page page = browser.newPage();<\/p>\n<p>page.navigate(&#8220;https:\/\/the-internet.herokuapp.com\/iframe&#8221;);<\/p>\n<p>Locator frame1 = page.frameLocator(&#8220;#mce_0_ifr&#8221;).locator(&#8220;html&#8221;);<\/p>\n<p>frame1.click();<\/p>\n<p>frame1.type(&#8220;https:\/\/rahulshettyacademy.com&#8221;);<\/p>\n<p>page.pause();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/p>\n<p>Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java to handle frames. Topics that we will cover: Fundamental overview of frames Handle frames Source code Fundamental overview of frames&nbsp; A frame refers to an HTML page that is inserted into another page.&nbsp; Launch https:\/\/paytm.com\/ and hit \u2018Sign In\u2019 button &nbsp; A distinct window pops up [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3471"}],"version-history":[{"count":8,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3471\/revisions"}],"predecessor-version":[{"id":3739,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3471\/revisions\/3739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}