How to use RelativeLocator class of org.openqa.selenium.support.locators package

Best Selenium code snippet using org.openqa.selenium.support.locators.RelativeLocator

Source:LearnLocators.java Github

copy

Full Screen

2import java.util.List;3import org.openqa.selenium.By;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.support.locators.RelativeLocator;7import org.openqa.selenium.support.locators.RelativeLocator.RelativeBy;8import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;9import static org.openqa.selenium.By.id;10public class LearnLocators {11 public static void main(String[] args) {12 // Set the ChromeDriver Exe Path13 System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");14 // Launch your browser15 ChromeDriver driver = new ChromeDriver();16 17 // 18 driver.get("http://leafground.com/pages/sorttable.html");19 // Find the new way20// WebElement thisElement = driver.findElement(RelativeLocator.withTagName("button").above(By.xpath("//*[text()='Get Position']")));21 /*WebElement thisElement = driver.findElement(22 RelativeLocator.withTagName("button").23 below(By.xpath("//*[text()='Get Position']")));*/24 25/* driver.findElement(id(""));26*/ 27 28 /*driver.findElement(RelativeBy.);29 */30 /*List<WebElement> allBelowElements = driver.findElements(31 RelativeLocator.withTagName("*").32 below(By.xpath("//*[text()='Get Position']")));33 34 // Print the text of the element35 for (WebElement thisElement : allBelowElements) {36 System.out.println(thisElement.getText());37 }38 */39 40 41 /*List<WebElement> belowElements = driver.findElements(RelativeLocator42 .withTagName("input")43 .above(By.xpath("//label[text()='Clear the text']")));44 45 System.out.println(belowElements.size());46 for (WebElement ele : belowElements) {47 System.out.println(ele.getAttribute("name"));48 ele.clear();49 }*/50 51 /*List<WebElement> elements = driver.findElements(RelativeLocator52 .withTagName("td")53 .toRightOf(By.xpath("//td[text()='100%']")));54 55 56 System.out.println(elements.size());57 for (WebElement ele : elements) {58 System.out.println(ele.getText());59 }*/60 61 62 63 List<WebElement> belowElements = driver.findElements(RelativeLocator64 .withTagName("td")65 .toRightOf(By.xpath("//td[text()='1001']")));66 67 System.out.println(belowElements.size());68 69 70 71 72 for (WebElement ele : belowElements) {73 System.out.println(ele.getText());74 75 } 76 77 ...

Full Screen

Full Screen

Source:ScaleFocusHomePage.java Github

copy

Full Screen

1package pages;2import org.openqa.selenium.*;3import org.openqa.selenium.support.FindBy;4import org.openqa.selenium.support.PageFactory;5import org.openqa.selenium.support.locators.RelativeLocator;6import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;7public class ScaleFocusHomePage {8 protected WebDriver driver;9 public ScaleFocusHomePage(WebDriver driver) {10 this.driver = driver;11 PageFactory.initElements(driver, this);12 }13 // SELECTORS14 @FindBy(css = ".icons-menu .icon-share.open-share")15 protected WebElement shareNavigationIcon;16 @FindBy(className = "search-container")17 protected WebElement searchContainer;18 @FindBy(css = ".gtm-hp-vmbtn-serv.view-more-btn")19 protected WebElement servicesViewMoreButton;20 @FindBy(css = ".desciprtion .h3-p")21 protected WebElement descriptionServicesParagraph;22 // METHODS23 public void clickSearchIconFromNavigation() {24 // Click to right of Careers in the Navigation drop down menu25 driver.findElement(RelativeLocator.withTagName("i")26 .toLeftOf(shareNavigationIcon))27 .click();28 }29 public String getSearchContainerAttribute(String attribute) {30 // Return element's passed attribute31 return searchContainer.getAttribute(attribute);32 }33 public void clickCloudServicesViewMoreButton() {34 // To remove "RelativeLocator." use: import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;35 driver.findElement(withTagName("span")36 .toRightOf(servicesViewMoreButton)37 .below(descriptionServicesParagraph))38 .click();39 }40}

Full Screen

Full Screen

Source:Chapter3_RelativeLocators.java Github

copy

Full Screen

...3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;7import org.openqa.selenium.support.locators.RelativeLocator;8import static org.openqa.selenium.support.locators.RelativeLocator.with;9import org.testng.annotations.AfterMethod;10import org.testng.annotations.BeforeMethod;11import org.testng.annotations.Test;12import io.github.bonigarcia.wdm.WebDriverManager;13public class Chapter3_RelativeLocators {14 WebDriver driver;15 @BeforeMethod16 public void setUp() {17 WebDriverManager.chromedriver().setup();18 driver = new ChromeDriver();19 driver.manage().window().maximize();20 driver.get("https://opensource-demo.orangehrmlive.com");21 }22 @Test23 public void testRelativeLocator() {24 WebElement loginPanel = driver.findElement(By.id("logInPanelHeading"));25 WebElement credentials = driver.findElement(RelativeLocator.with(By.tagName("span")).above(loginPanel));26 System.out.println(credentials.getText());27 }28 @Test29 public void testListOfElements() {30 List<WebElement> allSocialMedia = driver.findElements(with(By.tagName("img")).near(By.id("footer")));31 for (WebElement socialMedia : allSocialMedia) {32 System.out.println(socialMedia.getAttribute("alt"));33 }34 }35 @AfterMethod36 public void tearDown() {37 driver.quit();38 }39}...

Full Screen

Full Screen

Source:Test153.java Github

copy

Full Screen

1package stevejobspro;2import org.openqa.selenium.By;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.support.locators.RelativeLocator;6import org.openqa.selenium.support.locators.RelativeLocator.RelativeBy;7import io.github.bonigarcia.wdm.WebDriverManager;8public class Test153 {9 public static void main(String[] args) throws Exception10 {11 //open Browser 12 WebDriverManager.chromedriver().setup();13 ChromeDriver driver=new ChromeDriver();14 driver.manage().window().maximize();15 //lunch site under testing(SUT)16 driver.get("http://www.gmail.com");17 Thread.sleep(5000);18 WebElement e1=driver.findElement(By.xpath("//*[text()='Continue to Gmail']"));19 WebElement e2=driver.findElement(By.xpath("//*[text()='Forgot email?']"));20 WebElement target1=driver.findElement(RelativeLocator.withTagName("input").below(e1).above(e2));21 target1.sendKeys("chanikyareddy231");22 Thread.sleep(5000);23 WebElement e3=driver.findElement(By.xpath("//*[text()='Create account']"));24 RelativeLocator.withTagName("button").near(e3);25 WebElement target2=driver.findElement(RelativeBy.xpath("//*[@type='button']"));26 target2.click();27 }28}...

Full Screen

Full Screen

Source:RelativeTests.java Github

copy

Full Screen

1//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;2import io.github.bonigarcia.wdm.WebDriverManager;3import org.junit.Test;4import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.chrome.ChromeDriver;8import static org.openqa.selenium.By.tagName;9import static org.openqa.selenium.support.locators.RelativeLocator.with;10public class RelativeTests {11 @Test12 public void X(){13 WebDriverManager.chromedriver().setup();14 WebDriver driver = new ChromeDriver();15 driver.navigate().to("https://www.swtestacademy.com");16 WebElement logo = driver.findElement(with(tagName("article")).toRightOf(By.id("blog-1-post-8383")));17 System.out.println(logo.findElement(By.cssSelector(".entry-title")).getText());18 logo = driver.findElement(with(tagName("article")).below(By.id("blog-1-post-8405")));19 System.out.println(logo.findElement(By.cssSelector(".entry-title")).getText());20 }21}...

Full Screen

Full Screen

Source:TestRelativeLocators.java Github

copy

Full Screen

1package testcases;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5//--import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;6import org.openqa.selenium.support.locators.RelativeLocator;7import io.github.bonigarcia.wdm.WebDriverManager;8public class TestRelativeLocators {9 public static void main(String[] args) {10 // TODO Auto-generated method stub11 12 WebDriverManager.chromedriver().setup();13 14 WebDriver driver=new ChromeDriver();15 16 driver.get("http://way2automation.com/way2auto_jquery/index.php");17 driver.findElement(By.name("email")).sendKeys("rohanprag@gmail.com");18 //driver.findElement(By.xpath("//*[@id=\"load_form\"]/div/div[2]/input")).click();19 driver.findElement(RelativeLocator.withTagName("input").below(By.xpath("//*[@id=\"load_form\"]/fieldset[7]/input"))).click();20 System.out.println("Sign on clicked");21 22 23 }24}...

Full Screen

Full Screen

Source:Initiatepassword.java Github

copy

Full Screen

1package autotest.automate.webapp.influencer.pom;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.support.locators.RelativeLocator;5import org.openqa.selenium.support.locators.RelativeLocator.RelativeBy;6public class Initiatepassword {7 8 protected WebDriver driver;9 10 private By email = By.id("email");11 private By confirmEmail = By.id("confirm_email");12 private RelativeBy btnSend = RelativeLocator.withTagName("button").below(confirmEmail);13 private RelativeBy backSignIn = RelativeLocator.withTagName("button").below(btnSend);14 public Initiatepassword(WebDriver driver) {15 super();16 this.driver = driver;17 }18 19}...

Full Screen

Full Screen

Source:DifferentImportStatements.java Github

copy

Full Screen

1package io.testproject.blog11selenium4relativelocators;23import static org.openqa.selenium.support.locators.RelativeLocator.*;4import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;56import org.openqa.selenium.WebDriver;7import org.openqa.selenium.support.locators.RelativeLocator;8import org.testng.annotations.Test;910public class DifferentImportStatements {1112 WebDriver driver;13 14 @Test15 public void useRelativeLocatorInSyntax () {16 driver.findElement(RelativeLocator.withTagName(""));17 }18 19 @Test20 public void skipRelativeLocatorInSyntax () {21 driver.findElement(withTagName(""));22 }23}24 ...

Full Screen

Full Screen

RelativeLocator

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.support.locators.RelativeLocator;6import java.util.concurrent.TimeUnit;7public class RelativeLocatorExample {8 public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe");10 WebDriver driver = new ChromeDriver();11 driver.manage().window().maximize();12 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);13 WebElement searchBox = driver.findElement(By.id("twotabsearchtextbox"));14 searchBox.sendKeys("Iphone");15 WebElement searchButton = driver.findElement(RelativeLocator.withTagName("input").toRightOf(searchBox));16 searchButton.click();17 }18}19import org.openqa.selenium.By;20import org.openqa.selenium.WebDriver;21import org.openqa.selenium.WebElement;22import org.openqa.selenium.chrome.ChromeDriver;23import org.openqa.selenium.support.ui.ExpectedConditions;24import org.openqa.selenium.support.ui.RelativeLocator;25import org.openqa.selenium.support.ui.WebDriverWait;26import java.util.concurrent.TimeUnit;27public class RelativeLocatorExample {28 public static void main(String[] args) {29 System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe");30 WebDriver driver = new ChromeDriver();31 driver.manage().window().maximize();32 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);33 WebElement searchBox = driver.findElement(By.id("twotabsearchtextbox"));34 searchBox.sendKeys("Iphone");35 WebDriverWait wait = new WebDriverWait(driver, 10);36 WebElement searchButton = wait.until(ExpectedConditions.elementToBeClickable(RelativeLocator.withTagName("input").toRightOf(searchBox)));37 searchButton.click();38 }39}

Full Screen

Full Screen

RelativeLocator

Using AI Code Generation

copy

Full Screen

1package com.packt.webdriver.chapter3;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.firefox.FirefoxDriver;6import org.openqa.selenium.support.locators.RelativeLocator;7public class RelativeLocatorClass {8 public static void main(String... args){9 WebDriver driver = new FirefoxDriver();10 WebElement gmailLink = driver.findElement(RelativeLocator.withTagName("a").toRightOf(By.id("gb_70")));11 System.out.println(gmailLink.getText());12 }13}14toRightOf(By by)15toLeftOf(By by)16above(By by)17below(By by)18near(By by)19withTagName(String tagName)20withClassName(String className)21withCssSelector(String cssSelector)22withXPath(String xpath)23withName(String name)24withId(String id)25withLinkText(String linkText)26withPartialLinkText(String partialLinkText)27withTagName(String tagName)28withClassName(String className)29withCssSelector(String cssSelector)30withXPath(String xpath)31withName(String name)32withId(String id)33withLinkText(String linkText)34withPartialLinkText(String partialLinkText)35withTagName(String tagName)36withClassName(String className)37withCssSelector(String cssSelector)38withXPath(String xpath)39withName(String name)40withId(String id)41withLinkText(String linkText)42withPartialLinkText(String partialLinkText)43withTagName(String tagName)44withClassName(String className)45withCssSelector(String cssSelector)46withXPath(String xpath)47withName(String name)48withId(String id)49withLinkText(String linkText)50withPartialLinkText(String partialLinkText)51withTagName(String tagName)52withClassName(String className)53withCssSelector(String cssSelector)54withXPath(String xpath)55withName(String name)56withId(String id)57withLinkText(String linkText)58withPartialLinkText(String partialLinkText)59withTagName(String tagName)60withClassName(String className)61withCssSelector(String cssSelector)62withXPath(String xpath)63withName(String name)64withId(String id)65withLinkText(String linkText)66withPartialLinkText(String partialLinkText)67withTagName(String tagName

Full Screen

Full Screen

RelativeLocator

Using AI Code Generation

copy

Full Screen

1WebElement element = driver.findElement(By.id("b")).findElement(2 new RelativeLocator.Builder(driver.findElement(By.id("b"))).nextSibling().build());3WebElement element = driver.findElement(By.id("b")).findElement(4 new RelativeLocator.Builder(driver.findElement(By.id("b"))).followingSibling().build());5WebElement element = driver.findElement(By.id("b")).findElement(6 new RelativeLocator.Builder(driver.findElement(By.id("b"))).nextSibling(By.id("c")).build());7WebElement element = driver.findElement(By.id("b")).findElement(8 new RelativeLocator.Builder(driver.findElement(By.id("b"))).followingSibling(By.id("c")).build());9WebElement element = driver.findElement(By.id("b")).findElement(10 new RelativeLocator.Builder(driver.findElement(By.id("b"))).previousSibling().build());11WebElement element = driver.findElement(By.id("b")).findElement(12 new RelativeLocator.Builder(driver.findElement(By.id("b"))).precedingSibling().build());13WebElement element = driver.findElement(By.id("b")).findElement(14 new RelativeLocator.Builder(driver.findElement(By.id("b"))).previousSibling(By.id("a")).build());

Full Screen

Full Screen
copy
1long numberOfPages = new BigDecimal(resultsSize).divide(new BigDecimal(pageSize), RoundingMode.UP).longValue();2
Full Screen
copy
1public static long divideAndRoundUp(long num, long divisor) {2 if (num == 0 || divisor == 0) { return 0; }34 int sign = (num > 0 ? 1 : -1) * (divisor > 0 ? 1 : -1);56 if (sign > 0) {7 return (num + divisor - 1) / divisor;8 }9 else {10 return (num / divisor);11 }12}13
Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in RelativeLocator

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful