In this tutorial, I will list out Selenium WebElement locator. WebElement’s locators used to find all WebElements within a web page.
Selenium WebElement Locator
1. findElements() - This method is used to find all WebElements within a web page with same locator.
Example: List<WebElement> element = driver.findElements(By.name(“q”));
2. findElement() - This method is used to find the first WebElement within a web page.
Example: WebElement element = driver.findElement(By.name(“q”));
3. By ID - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s id.
Example: WebElement element = driver.findElement(By.id(“q”));
4. By Name - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s name.
Example: WebElement element = driver.findElement(By.name(“q”));
5. By Tag Name - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s tagName.
Example: WebElement frame = driver.findElement(By.tagName(“q”));
6. By XPATH - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s xpath.
Example: WebElement element = driver.findElements(By.xpath(“//input”));
7. By CSS - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s css.
Example: WebElement element = driver.findElement(By.cssSelector(“#q span.text.name”));
8. By Class Name – This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s className.
Example: List<WebElement> element = driver.findElements(By.className(“q”));
9. By Link Text - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s linkText.
Example: WebElement element = driver.findElement(By.linkText(“q”));
10. By Partial Link Text - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s partialLinkText.
Example: WebElement element = driver.findElement(By.partialLinkText(“q”));
Example: List<WebElement> element = driver.findElements(By.name(“q”));
2. findElement() - This method is used to find the first WebElement within a web page.
Example: WebElement element = driver.findElement(By.name(“q”));
3. By ID - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s id.
Example: WebElement element = driver.findElement(By.id(“q”));
4. By Name - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s name.
Example: WebElement element = driver.findElement(By.name(“q”));
5. By Tag Name - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s tagName.
Example: WebElement frame = driver.findElement(By.tagName(“q”));
6. By XPATH - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s xpath.
Example: WebElement element = driver.findElements(By.xpath(“//input”));
7. By CSS - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s css.
Example: WebElement element = driver.findElement(By.cssSelector(“#q span.text.name”));
8. By Class Name – This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s className.
Example: List<WebElement> element = driver.findElements(By.className(“q”));
9. By Link Text - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s linkText.
Example: WebElement element = driver.findElement(By.linkText(“q”));
10. By Partial Link Text - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s partialLinkText.
Example: WebElement element = driver.findElement(By.partialLinkText(“q”));