Locators In Selenium WebDriver (Java/Python)

Locators In Selenium WebDriver (Java/Python)

In selenium, you can only locate elements using locators, commands which when provided the correct input, will tell the webdriver which element on the page to look for. The locators can be chosen based on which search condition you want to use, for certain commonly used tags there will be locators available specifically for them such as ID, link text or name, etc. Each language has its own way of using locators, For example:

WebElement element = driver.findElement(By.id("Some ID")); //Java
element = driver.find_element_by_id("Some ID") //Python

For more customisable locators you can search by XPath or CssSelector. These locators allow very specific searches based on multiple conditions at once including: Any attribute of an element, the position of the element relative to other elements, or even the type of html tag the element uses. E.g:

By locator = By.xpath("//div[contains(id, 'searchInput')];

In this example the locator will return an element where part of its ID matches the string "searchInput".

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.