Skip to main content

Posts

Showing posts with the label RelativeBy

RelativeBy Locators in Selenium

Have you started using RelativeBy Locators (Friendly locators) in Selenium 4 (alpha-3) yet or not? If not, then you should give it a try. These new locator methods help you find elements based on their visual location relative to other elements in the DOM. As of now, it supports with "withTagName" attribute and allows the following: - above - below - near - toleftOf - toRightOf  It supports both By and WebElement. Sample syntax:  WebElement unameLabel = driver.findElement(By.cssSelector("label[id=''uname]")); WebElement username =  driver.findElement(withTagName("input").toRightOf(unameLabel)); or you can use: WebElement username =  driver.findElement(withTagName("input").toRightOf(driver.findElement(By.cssSelector("label[id=''uname]")));