{"id":3471,"date":"2025-01-16T19:07:33","date_gmt":"2025-01-16T19:07:33","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/handling-frames-in-playwright-java\/"},"modified":"2025-01-16T19:07:33","modified_gmt":"2025-01-16T19:07:33","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><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3471\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will be utilizing Playwright Java to handle frames.<\/em><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\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\u00a0<\/strong><\/p>\n<p>A frame refers to an HTML page that is inserted into another page.\u00a0<\/p>\n<p>Launch <a href=\"https:\/\/paytm.com\/\" rel=\"noopener\" target=\"_blank\">https:\/\/paytm.com\/<\/a> and hit \u2018Sign In\u2019 button<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194708489cf\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3476\" data-css=\"tve-u-1947084a046\" data-height=\"297\" data-id=\"3476\" data-init-height=\"744\" data-init-width=\"1198\" data-width=\"478\" decoding=\"async\" height=\"297\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.38.11\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 744;\" title=\"Screenshot 2025-01-17 at 12.38.11\u202fAM\" width=\"478\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0 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?\u00a0<\/p>\n<p>By clicking the right mouse button on this window, the option to view the frame source becomes visible<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470856322\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3477\" data-css=\"tve-u-19470857495\" data-height=\"398\" data-id=\"3477\" data-init-height=\"858\" data-init-width=\"1198\" data-width=\"556\" decoding=\"async\" height=\"398\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.38.57\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 858;\" title=\"Screenshot 2025-01-17 at 12.38.57\u202fAM\" width=\"556\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947085dfbe\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3478\" data-css=\"tve-u-1947085f254\" data-height=\"297\" data-id=\"3478\" data-init-height=\"696\" data-init-width=\"1198\" data-width=\"511\" decoding=\"async\" height=\"297\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.39.41\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 696;\" title=\"Screenshot 2025-01-17 at 12.39.41\u202fAM\" width=\"511\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This 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 class=\"\" href=\"https:\/\/the-internet.herokuapp.com\/iframe\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/the-internet.herokuapp.com\/iframe<\/a>\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470868cb8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3479\" data-css=\"tve-u-1947086d355\" data-height=\"291\" data-id=\"3479\" data-init-height=\"760\" data-init-width=\"1198\" data-width=\"458\" decoding=\"async\" height=\"291\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.40.17\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 760;\" title=\"Screenshot 2025-01-17 at 12.40.17\u202fAM\" width=\"458\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let&#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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194708793af\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3480\" data-css=\"tve-u-1947087a7c7\" data-height=\"717\" data-id=\"3480\" data-init-height=\"1484\" data-init-width=\"1198\" data-width=\"579\" decoding=\"async\" height=\"717\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.41.16\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 1484;\" title=\"Screenshot 2025-01-17 at 12.41.16\u202fAM\" width=\"579\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-19470885a46\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3481\" data-css=\"tve-u-194708875fd\" data-height=\"395\" data-id=\"3481\" data-init-height=\"952\" data-init-width=\"1198\" data-width=\"497\" decoding=\"async\" height=\"395\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.42.18\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 952;\" title=\"Screenshot 2025-01-17 at 12.42.18\u202fAM\" width=\"497\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>This is how our code looks<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1947089ef42\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3482\" data-css=\"tve-u-194708a0c9e\" data-height=\"252\" data-id=\"3482\" data-init-height=\"656\" data-init-width=\"1198\" data-width=\"461\" decoding=\"async\" height=\"252\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.44.04\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 656;\" title=\"Screenshot 2025-01-17 at 12.44.04\u202fAM\" width=\"461\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>The text appears in the text area of the frame as shown below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-194708a7543\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3483\" data-css=\"tve-u-194708a8d18\" data-height=\"296\" data-id=\"3483\" data-init-height=\"656\" data-init-width=\"1198\" data-width=\"541\" decoding=\"async\" height=\"296\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-17-at-12.44.42\u202fAM.png\" style=\"aspect-ratio: auto 1198 \/ 656;\" title=\"Screenshot 2025-01-17 at 12.44.42\u202fAM\" width=\"541\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Playwright does not require switching to a frame, unlike Selenium.\u00a0<\/p>\n<p><strong>Source code<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/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><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog27_iframes_PWJava_Example1 {<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/p>\n<p>Playwright playwright = Playwright.<em>create<\/em>();\u00a0<\/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><\/p>\n<p>Thanks<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/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\u00a0 A frame refers to an HTML page that is inserted into another page.\u00a0 Launch https:\/\/paytm.com\/ and hit \u2018Sign In\u2019 button \u00a0 A distinct window pops up [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3471","post","type-post","status-publish","format-standard","hentry","category-java","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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3471\/revisions"}],"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}]}}