Selenium offers a number of build in selectors to easy find elements in the application under test
| class name | Locates elements whose class name contains the search value (compound class names are not permitted) |
| css selector | Locates elements matching a CSS selector |
| id | Locates elements whose ID attribute matches the search value |
| name | Locates elements whose NAME attribute matches the search value |
| link text | Locates anchor elements whose visible text matches the search value |
| partial link text | Locates anchor elements whose visible text contains the search value. If multiple elements are matching, only the first one will be selected. |
| tag name | Locates elements whose tag name matches the search value |
| xpath | Locates elements matching an XPath expression |
Preferred Selector Order
id > name >links text> css > xpath
// locate google search field using ID
driver.findElement(By.id("lst-ib"));
// locate google search field using name
driver.findElement(By.name("q"));
// locate google search field using Class name
driver.findElement(By.className("gsfi"));
// locate google search field using css selector
driver.findElement(By.cssSelector("#lst-ib"));
// locate google search field using link Text
driver.findElement(By.linkText("Gmail"));
// locate google search field using partial link text
driver.findElement(By.partialLinkText("mail"));
// locate google search field using tag name
driver.findElement(By.tagName("input"));
// locate google search field using xpath
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]"));

Leave a Reply
You must be logged in to post a comment.