Skip to main content

Posts

Showing posts with the label Locators

3 Selenium classes that are not so popular to locate elements

Here we have discussed 3 Selenium classes that are not so popular but that can help you locate elements in Selenium: - ByIdOrName - ByChained - ByAll 1) ByIdOrName - This helps the driver to locate an element either by name or by id. This is present under org.openqa.selenium.support package and to use this we have to call the constructor of the ByIdOrName class and pass the Name or ID. This method tries to find the element using ID first and it waits till the max implicit wait time for the element to locate using ID and if it is not able to find the element with ID then only it tries to locate with the Name. As soon as the driver finds the element with id, it will not check the element with a name attribute and won't want for max implicit wait time either. driver.findElement(new ByIdOrName("username")).click(); 2) ByChained - This helps the driver to locate the element based on the parent element, and it accepts an unlimited number of locators. This is prese...

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]")));