Selenium 4 - Relative Locators
Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from intent to execution in minutes.
|
x
Blog
Selenium 4 - Comparative Locators
Selenium 4 volunteer a way of place elements by using natural language terms. This article describe how to use the new Relative Locators.
This functionality brings a new way to to assist you find the ones that are nearby former elements. The available locators are:
abovebelowtoLeftOftoRightOfnear
The concept behind this method is to allow you to find ingredient based on how one would describe them on a page. It is more natural to say: “ find the element that is below the image ”, rather than “ find the INPUT inside the DIV with the ‘ id ’ of ‘ main ’ ”. In general, you could think some them as a way to locate elements based on the optical position on the page.
For extra illustration, include chaining relative locator in these speech, look at our.
We will use the following login form to understand how Relative Locators work:

Above
We would like to observe the email speech field, which is above the watchword field. To do that, we find the password field through its id, and then we use the above relative locator.
1Java23importstatic org.openqa.selenium.support.locators.RelativeLocator.with;4WebElement password = driver.findElement(By.id(& quot; countersign & quot;));5WebElement email = driver.findElement(with(By.tagName(& quot; input & quot;)).above(passwordField));67Python89from selenium.webdriver.common.byimportBy10from selenium.webdriver.support.relative_locatorimportlocate_with11passwordField= driver.find_element(By.ID,& quot; password & quot;)12emailAddressField= driver.find_element(locate_with(By.TAG_NAME,& quot; input & quot;).above(passwordField))1314C#1516using staticOpenQA.Selenium.RelativeBy;17IWebElementpasswordField= driver.FindElement(By.Id(& quot; countersign & quot;));18IWebElementemailAddressField= driver.FindElement(RelativeBy(By.TagName(& quot; input & quot;)).Above(passwordField));1920Ruby2122password_field= driver.find_element(:id,& quot; password & quot;)23email_address_field= driver.find_element(relative:{tag_name:& quot; comment & quot;,above:password_field})2425JavaScript2627letpasswordField= driver.findElement(By.id(& # x27; password & # x27;));28letemailAddressField=await driver.findElement(locateWith(By.tagName(& # x27; input & # x27;)).above(passwordField));29
Below
Going the former way around, let & # x27; s chance the password field, which is below the e-mail reference field.
1Java2importstatic org.openqa.selenium.support.locators.RelativeLocator.with;3WebElementemailAddressField= driver.findElement(By.id(& quot; email & quot;));4WebElementpasswordField= driver.findElement(with(By.tagName(& quot; input & quot;))5.below(emailAddressField));67Python89from selenium.webdriver.common.byimportBy10from selenium.webdriver.support.relative_locatorimportlocate_with11emailAddressField= driver.find_element(By.ID,& quot; email & quot;)12passwordField= driver.find_element(locate_with(By.TAG_NAME,& quot; input & quot;).below(emailAddressField))1314C#1516using staticOpenQA.Selenium.RelativeBy;17IWebElementemailAddressField= driver.FindElement(By.Id(& quot; email & quot;));18IWebElementpasswordField=19driver.FindElement(RelativeBy(By.TagName(& quot; remark & quot;)).Below(emailAddressField));2021Ruby2223email_address_field= driver.find_element(:id,& quot; email & quot;)24password_field= driver.find_element(relative:{tag_name:& quot; comment & quot;,below:email_address_field})2526JavaScript2728letemailAddressField= driver.findElement(By.id(& # x27; email & # x27;));29letpasswordField=await driver.findElement(locateWith(By.tagName(& # x27; input & # x27;)).below(emailAddressField));30
To the Left Of
Let ’ s consider the event where we want to regain the ingredient on the left side of the “ Submit ” push.
1Java23importstatic org.openqa.selenium.support.locators.RelativeLocator.with;4WebElementsubmitButton= driver.findElement(By.id(& quot; submit & quot;));5WebElementcancelButton= driver.findElement(with(By.tagName(& quot; button & quot;))6.toLeftOf(submitButton));78Python910from selenium.webdriver.common.byimportBy11from selenium.webdriver.support.relative_locatorimportlocate_with12submitButton= driver.find_element(By.ID,& quot; submit & quot;)13cancelButton= driver.find_element(locate_with(By.TAG_NAME,& quot; button & quot;).14to_left_of(submitButton))1516C#1718using staticOpenQA.Selenium.RelativeBy;19IWebElementemailAddressField= driver.FindElement(By.Id(& quot; email & quot;));20IWebElementpasswordField=21driver.FindElement(RelativeBy(By.TagName(& quot; input & quot;)).Below(emailAddressField));2223Ruby2425submit_button= driver.find_element(:id,& quot; submit & quot;)26cancel_button= driver.find_element(relative:{tag_name:& # x27; button & # x27;,left:submit_button})2728JavaScript2930letsubmitButton= driver.findElement(By.id(& # x27; submit & # x27;));31letcancelButton=await driver.findElement(locateWith(By.tagName(& # x27; button & # x27;)).toLeftOf(submitButton));32SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.
To the Right Of
Now we & # x27; ll consider the paired case, where we want to observe the element on the correct side of the “ Cancel ” button.
1Java23importstatic org.openqa.selenium.support.locators.RelativeLocator.with;4WebElementcancelButton= driver.findElement(By.id(& quot; natural & quot;));5WebElementsubmitButton= driver.findElement(with(By.tagName(& quot; button & quot;)).toRightOf(cancelButton));67Python89from selenium.webdriver.common.byimportBy10from selenium.webdriver.support.relative_locatorimportlocate_with11cancelButton= driver.find_element(By.ID,& quot; cancel & quot;)12submitButton= driver.find_element(locate_with(By.TAG_NAME,& quot; button & quot;).13to_right_of(cancelButton))1415C#1617using staticOpenQA.Selenium.RelativeBy;18IWebElementcancelButton= driver.FindElement(By.Id(& quot; cancel & quot;));19IWebElementsubmitButton= driver.FindElement(RelativeBy(By.TagName(& quot; button & quot;)).RightOf(cancelButton));2021Ruby2223cancel_button= driver.find_element(:id,& quot; natural & quot;)24submit_button= driver.find_element(relative:{tag_name:& # x27; button & # x27;,right:cancel_button})2526JavaScript2728letcancelButton= driver.findElement(By.id(& # x27; cancel & # x27;));29letsubmitButton=await driver.findElement(locateWith(By.tagName(& # x27; button & # x27;)).toRightOf(cancelButton));30
Near
nearis helpful when we need to find an element that is at most 50px off from the fix constituent. In this case, we would like to find the email speech battlefield by first finding the label of that battlefield.
1Java23importstatic org.openqa.selenium.support.locators.RelativeLocator.with;4WebElementemailAddressLabel= driver.findElement(By.id(& quot; lbl-email & quot;));5WebElementemailAddressField= driver.findElement(with(By.tagName(& quot; input & quot;)).near(emailAddressLabel));67Python89from selenium.webdriver.common.byimportBy10from selenium.webdriver.support.relative_locatorimportlocate_with11emailAddressLabel= driver.find_element(By.ID,& quot; lbl-email & quot;)12emailAddressField= driver.find_element(locate_with(By.TAG_NAME,& quot; stimulus & quot;).13near(emailAddressLabel))1415C#1617using staticOpenQA.Selenium.RelativeBy;18IWebElementemailAddressLabel= driver.FindElement(By.Id(& quot; lbl-email & quot;));19IWebElementemailAddressField= driver.FindElement(RelativeBy(By.TagName(& quot; input & quot;)).Near(emailAddressLabel));2021Ruby2223email_address_label= driver.find_element(:id,& quot; lbl-email & quot;)24email_address_field= driver.find_element(relative:{tag_name:& # x27; input & # x27;,near:email_address_label})2526JavaScript2728letemailAddressLabel= driver.findElement(By.id(& quot; lbl-email & quot;));29letemailAddressField=await driver.findElement(with(By.tagName(& quot; input & quot;)).near(emailAddressLabel));30
An updated set of method, utilities and exemplar can be found at theofficial documentation.
Check out our comprehensive guidebook to for more information.
Related resources
Sr. Developer Experience Engineer, Sauce Labs
Staff Software Engineer at Sauce Labs
Share this post
Ready to start testing with Selenium and Sauce Labs?
Let & # x27; s go!
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free


