{"id":3175,"date":"2025-01-15T19:49:37","date_gmt":"2025-01-15T19:49:37","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/15\/relative-locators-in-playwright-java-below-above-near-right-of-left-of\/"},"modified":"2025-01-15T19:49:37","modified_gmt":"2025-01-15T19:49:37","slug":"relative-locators-in-playwright-java-below-above-near-right-of-left-of","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/15\/relative-locators-in-playwright-java-below-above-near-right-of-left-of\/","title":{"rendered":"Relative Locators in Playwright Java (below, above, near, right-of, left-of)"},"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=\"3175\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog, we will explore Playwright Java&#8217;s relative locators, that can be used to locate elements positioned above\/below\/near\/right\/left of a given element.<\/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>Usage of Relative Locator \u2018below\u2019<\/li>\n<li>Usage of Relative Locator \u2018above\u2019<\/li>\n<li>Usage of Relative Locator \u2018near\u2019<\/li>\n<li>Usage of Relative Locator \u2018right-of\u2019<\/li>\n<li>Usage of Relative Locator \u2018left-of\u2019<\/li>\n<li>Source code<\/li>\n<\/ul>\n<p><strong>Usage of Relative Locator \u2018below\u2019\u00a0<\/strong><\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/opensource-demo.orangehrmlive.com\/\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/opensource-demo.orangehrmlive.com\/<\/a>\u00a0<\/p>\n<p>As can be seen below, the input \u2018Username\u2019 field is \u201c<strong>below\u201d<\/strong> <br \/>\u2018Username\u2019 text label<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b880b7b\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3178\" data-css=\"tve-u-1946b8828d8\" data-height=\"181\" data-id=\"3178\" data-init-height=\"366\" data-init-width=\"1164\" data-width=\"575\" decoding=\"async\" height=\"181\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.23.55\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 366;\" title=\"Screenshot 2025-01-16 at 1.23.55\u202fAM\" width=\"575\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us try to enter text \u201crahulshettyacademy\u201d using \u201c<strong><em>below<\/em><\/strong>\u201d locator.\u00a0<\/p>\n<p>Line#26 will help us to achieve that. The \u2018Username\u2019 input field is <br \/>represented by \u2018input\u2019 tag and hence we have used \u2018input\u2019 tag just before \u201cbelow\u201d<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b890e81\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3179\" data-css=\"tve-u-1946b892294\" data-height=\"59\" data-id=\"3179\" data-init-height=\"110\" data-init-width=\"1164\" data-width=\"622\" decoding=\"async\" height=\"59\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.24.53\u202fAM.png\" style=\"aspect-ratio: auto 1164 \/ 110;\" title=\"Screenshot 2025-01-16 at 1.24.53\u202fAM\" width=\"622\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.\u00a0<\/p>\n<p>The text \u201crahulshettyacademy\u201d gets typed in the \u2018Username\u2019 input field<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b89adce\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3180\" data-css=\"tve-u-1946b89bc6d\" data-height=\"301\" data-id=\"3180\" data-init-height=\"422\" data-init-width=\"694\" data-width=\"495\" decoding=\"async\" height=\"301\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.25.46\u202fAM.png\" style=\"aspect-ratio: auto 694 \/ 422;\" title=\"Screenshot 2025-01-16 at 1.25.46\u202fAM\" width=\"495\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Usage of Relative Locator \u2018above\u2019\u00a0<\/strong><\/p>\n<p>Let us navigate to\u00a0 <a class=\"\" href=\"https:\/\/sso.teachable.com\/secure\/9521\/identity\/login\/password\" rel=\"noopener\" style=\"outline: none;\" target=\"_blank\">https:\/\/sso.teachable.com\/secure\/9521\/identity\/login\/password<\/a> \u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8a3e77\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3181\" data-css=\"tve-u-1946b8a5bff\" data-height=\"558\" data-id=\"3181\" data-init-height=\"712\" data-init-width=\"694\" data-width=\"544\" decoding=\"async\" height=\"558\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.26.19\u202fAM.png\" style=\"aspect-ratio: auto 694 \/ 712;\" title=\"Screenshot 2025-01-16 at 1.26.19\u202fAM\" width=\"544\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0As seen above, there is an \u2018Email\u2019 field <strong>above<\/strong> \u2018Password\u2019 field.<\/p>\n<p>We can apply the same logic in this scenario as well<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8b076f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3182\" data-css=\"tve-u-1946b8b1d59\" data-height=\"148\" data-id=\"3182\" data-init-height=\"262\" data-init-width=\"1140\" data-width=\"642\" decoding=\"async\" height=\"148\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.27.14\u202fAM.png\" style=\"aspect-ratio: auto 1140 \/ 262;\" title=\"Screenshot 2025-01-16 at 1.27.14\u202fAM\" width=\"642\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.<\/p>\n<p>Notice below that Playwright types the text in the \u2018Email\u2019 input text field\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8b8cc5\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3183 tcb-moved-image\" data-css=\"tve-u-1946b8b983f\" data-height=\"407\" data-id=\"3183\" data-init-height=\"784\" data-init-width=\"1140\" data-width=\"592\" decoding=\"async\" height=\"407\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.27.50\u202fAM.png\" style=\"aspect-ratio: auto 1140 \/ 784;\" title=\"Screenshot 2025-01-16 at 1.27.50\u202fAM\" width=\"592\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This is the way in which we can make use of the &#8220;<em>above<\/em>&#8221; method of the relative locator.<\/p>\n<p><strong>Usage of Relative Locator \u2018near\u2019\u00a0<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/rahulshettyacademy.com\/AutomationPractice\/\">https:\/\/rahulshettyacademy.com\/AutomationPractice\/<\/a> \u00a0<\/p>\n<p>As can be seen below, the dropdown field is \u201c<strong>near\u201d<\/strong> \u2018Dropdown\u2019 text<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8c3e49\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3184\" data-css=\"tve-u-1946b8c65ef\" data-height=\"291\" data-id=\"3184\" data-init-height=\"584\" data-init-width=\"1140\" data-width=\"568\" decoding=\"async\" height=\"291\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.28.28\u202fAM.png\" style=\"aspect-ratio: auto 1140 \/ 584;\" title=\"Screenshot 2025-01-16 at 1.28.28\u202fAM\" width=\"568\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us try to select \u201cOptions1\u201d using \u201c<strong><em>near<\/em><\/strong>\u201d locator. Line#27 will help us to achieve that. The dropdown is represented by \u2018select\u2019 tag and hence we have used \u2018select\u2019 tag just before \u201cnear\u201d<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8dd800\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3185\" data-css=\"tve-u-1946b8dec2a\" data-height=\"131\" data-id=\"3185\" data-init-height=\"204\" data-init-width=\"1140\" data-width=\"733\" decoding=\"async\" height=\"131\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.30.17\u202fAM.png\" style=\"aspect-ratio: auto 1140 \/ 204;\" title=\"Screenshot 2025-01-16 at 1.30.17\u202fAM\" width=\"733\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.\u00a0<\/p>\n<p>\u201cOption1\u201d gets selected<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8e7f8a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3186\" data-css=\"tve-u-1946b8e91cc\" data-height=\"408\" data-id=\"3186\" data-init-height=\"702\" data-init-width=\"1140\" data-width=\"663\" decoding=\"async\" height=\"408\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.30.59\u202fAM.png\" style=\"aspect-ratio: auto 1140 \/ 702;\" title=\"Screenshot 2025-01-16 at 1.30.59\u202fAM\" width=\"663\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Usage of Relative Locator \u2018right-of\u2019\u00a0<\/strong><\/p>\n<p>Go to <a class=\"\" href=\"https:\/\/rahulshettyacademy.com\/seleniumPractise\/#\/offers\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/seleniumPractise\/#\/offers<\/a> \u00a0<\/p>\n<p>As can be seen below, the dropdown field is \u201c<strong>right-of\u201d<\/strong> \u2018Page size\u2019 text<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8f16d2\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3187\" data-css=\"tve-u-1946b8f457d\" data-height=\"171\" data-id=\"3187\" data-init-height=\"306\" data-init-width=\"834\" data-width=\"465\" decoding=\"async\" height=\"171\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.31.40\u202fAM.png\" style=\"aspect-ratio: auto 834 \/ 306;\" title=\"Screenshot 2025-01-16 at 1.31.40\u202fAM\" width=\"465\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<\/p>\n<p>Let us try to select \u201c20\u201d using \u201c<strong><em>right-of<\/em><\/strong>\u201d locator. Line#27 will help us to achieve that. The dropdown is represented by \u2018select\u2019 tag and hence we have used \u2018select\u2019 tag just before \u201cright-of\u201d<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b8ff9f3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3188\" data-css=\"tve-u-1946b90340e\" data-height=\"491\" data-id=\"3188\" data-init-height=\"982\" data-init-width=\"1182\" data-width=\"591\" decoding=\"async\" height=\"491\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.32.29\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 982;\" title=\"Screenshot 2025-01-16 at 1.32.29\u202fAM\" width=\"591\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Usage of Relative Locator \u2018left-of\u2019<\/strong><\/p>\n<p>Launch <a class=\"\" href=\"https:\/\/rahulshettyacademy.com\/\" style=\"outline: none;\">https:\/\/rahulshettyacademy.com\/<\/a> \u00a0<\/p>\n<p>As can be seen below, the \u201cREGISTER\u201d link is \u201c<strong><em>left-of<\/em><\/strong><strong>\u201d<\/strong> LOGIN link<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b91139f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3189\" data-css=\"tve-u-1946b912948\" data-height=\"118\" data-id=\"3189\" data-init-height=\"206\" data-init-width=\"1182\" data-width=\"677\" decoding=\"async\" height=\"118\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.33.44\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 206;\" title=\"Screenshot 2025-01-16 at 1.33.44\u202fAM\" width=\"677\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Let us try to click \u201cREGISTER\u201d link using \u201c<strong><em>left-of<\/em><\/strong>\u201d locator. Line#27 will help us to achieve that. The link is represented by \u2018a\u2019 tag and hence we have used \u2018a\u2019 tag just before \u201cleft-of\u201d<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b918877\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3190\" data-css=\"tve-u-1946b9197f9\" data-height=\"124\" data-id=\"3190\" data-init-height=\"206\" data-init-width=\"1182\" data-width=\"713\" decoding=\"async\" height=\"124\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.34.21\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 206;\" title=\"Screenshot 2025-01-16 at 1.34.21\u202fAM\" width=\"713\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.<\/p>\n<p>The registration page opens up<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946b920273\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3191\" data-css=\"tve-u-1946b921156\" data-height=\"498\" data-id=\"3191\" data-init-height=\"868\" data-init-width=\"1182\" data-width=\"678\" decoding=\"async\" height=\"498\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-1.34.53\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 868;\" title=\"Screenshot 2025-01-16 at 1.34.53\u202fAM\" width=\"678\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<\/p>\n<p><\/p>\n<div class=\"tcb-plain-text\"><strong>Source code (below)<\/strong><\/div>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> org.junit.jupiter.api.Test;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Browser;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Locator;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Page;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog11_AboveMethod_UC1_below {<\/div>\n<div class=\"tcb-plain-text\">@Test<\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() {<\/div>\n<div class=\"tcb-plain-text\">Playwright pt = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\">LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/div>\n<div class=\"tcb-plain-text\">lo.setChannel(&#8220;msedge&#8221;);<\/div>\n<div class=\"tcb-plain-text\">lo.setHeadless(<strong>false<\/strong>);<\/div>\n<div class=\"tcb-plain-text\">BrowserType btype = pt.chromium();<\/div>\n<div class=\"tcb-plain-text\">Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/div>\n<div class=\"tcb-plain-text\">Page pg = b.newPage();<\/div>\n<div class=\"tcb-plain-text\">pg.navigate(&#8220;https:\/\/opensource-demo.orangehrmlive.com\/&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.locator(&#8220;input:below(:text(&#8220;Username&#8221;))&#8221;).first().fill(&#8220;rahulshettyacademy&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.pause();<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>Source code (above)<\/strong><\/div>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> org.junit.jupiter.api.Test;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Browser;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Locator;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Page;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog11_AboveMethod_UC2_above {<\/div>\n<div class=\"tcb-plain-text\">@Test<\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() {<\/div>\n<div class=\"tcb-plain-text\">Playwright pt = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\">LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/div>\n<div class=\"tcb-plain-text\">lo.setChannel(&#8220;msedge&#8221;);<\/div>\n<div class=\"tcb-plain-text\">lo.setHeadless(<strong>false<\/strong>);<\/div>\n<div class=\"tcb-plain-text\">BrowserType btype = pt.chromium();<\/div>\n<div class=\"tcb-plain-text\">Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/div>\n<div class=\"tcb-plain-text\">Page pg = b.newPage();<\/div>\n<div class=\"tcb-plain-text\">pg.navigate(&#8220;https:\/\/sso.teachable.com\/secure\/9521\/identity\/login\/password&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.locator(&#8220;input:above(:text(&#8220;Password&#8221;))&#8221;).first().fill(&#8220;rahulshettyacademy&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.pause();<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<p><strong>Source code (near)<\/strong><\/p>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> org.junit.jupiter.api.Test;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Browser;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Locator;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Page;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog11_AboveMethod_UC3_near {<\/div>\n<div class=\"tcb-plain-text\">@Test<\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() {<\/div>\n<div class=\"tcb-plain-text\">Playwright pt = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\">LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/div>\n<div class=\"tcb-plain-text\">lo.setChannel(&#8220;msedge&#8221;);<\/div>\n<div class=\"tcb-plain-text\">lo.setHeadless(<strong>false<\/strong>);<\/div>\n<div class=\"tcb-plain-text\">BrowserType btype = pt.chromium();<\/div>\n<div class=\"tcb-plain-text\">Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/div>\n<div class=\"tcb-plain-text\">Page pg = b.newPage();<\/div>\n<div class=\"tcb-plain-text\">pg.navigate(&#8220;https:\/\/rahulshettyacademy.com\/AutomationPractice\/&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.locator(&#8220;select:near(:text(&#8220;Dropdown&#8221;))&#8221;).selectOption(&#8220;Option1&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.pause();<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>Source code (right-of)<\/strong><\/div>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> org.junit.jupiter.api.Test;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Browser;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Locator;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Page;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog11_AboveMethod_UC4_rightof {<\/div>\n<div class=\"tcb-plain-text\">@Test<\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() {<\/div>\n<div class=\"tcb-plain-text\">Playwright pt = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\">LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/div>\n<div class=\"tcb-plain-text\">lo.setChannel(&#8220;msedge&#8221;);<\/div>\n<div class=\"tcb-plain-text\">lo.setHeadless(<strong>false<\/strong>);<\/div>\n<div class=\"tcb-plain-text\">BrowserType btype = pt.chromium();<\/div>\n<div class=\"tcb-plain-text\">Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/div>\n<div class=\"tcb-plain-text\">Page pg = b.newPage();<\/div>\n<div class=\"tcb-plain-text\">pg.navigate(&#8220;https:\/\/rahulshettyacademy.com\/seleniumPractise\/#\/offers&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.locator(&#8220;select:right-of(:text(&#8220;Search&#8221;))&#8221;).selectOption(&#8220;20&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.pause();<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>Source code (left-of)<\/strong><\/div>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> org.junit.jupiter.api.Test;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Browser;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.BrowserType.LaunchOptions;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Locator;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Page;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog11_AboveMethod_UC5_leftoff {<\/div>\n<div class=\"tcb-plain-text\">@Test<\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>void<\/strong> PlaywrightJTest() {<\/div>\n<div class=\"tcb-plain-text\">Playwright pt = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\">LaunchOptions lo = <strong>new<\/strong> LaunchOptions();<\/div>\n<div class=\"tcb-plain-text\">lo.setChannel(&#8220;msedge&#8221;);<\/div>\n<div class=\"tcb-plain-text\">lo.setHeadless(<strong>false<\/strong>);<\/div>\n<div class=\"tcb-plain-text\">BrowserType btype = pt.chromium();<\/div>\n<div class=\"tcb-plain-text\">Browser b = btype.launch((<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>)));<\/div>\n<div class=\"tcb-plain-text\">Page pg = b.newPage();<\/div>\n<div class=\"tcb-plain-text\">pg.navigate(&#8220;https:\/\/rahulshettyacademy.com\/&#8221;);<\/div>\n<div class=\"tcb-plain-text\">pg.locator(&#8220;a:left-of(:text(&#8220;LOGIN&#8221;))&#8221;).first().click();<\/div>\n<div class=\"tcb-plain-text\">pg.pause();<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\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 explore Playwright Java&#8217;s relative locators, that can be used to locate elements positioned above\/below\/near\/right\/left of a given element. Topics that we will cover: Usage of Relative Locator \u2018below\u2019 Usage of Relative Locator \u2018above\u2019 Usage of Relative Locator \u2018near\u2019 Usage of Relative Locator \u2018right-of\u2019 Usage of Relative Locator \u2018left-of\u2019 Source code [&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-3175","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\/3175","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=3175"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3175\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}