Selenium Selectors

Selenium offers a number of build in selectors to easy find elements in the application under test

class nameLocates elements whose class name contains the search value (compound class names are not permitted)
css selector Locates elements matching a CSS selector
idLocates elements whose ID attribute matches the search value
nameLocates elements whose NAME attribute matches the search value
link textLocates anchor elements whose visible text matches the search value
partial link textLocates anchor elements whose visible text contains the search value. If multiple elements are matching, only the first one will be selected.
tag nameLocates elements whose tag name matches the search value
xpathLocates 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\"]"));

Posted

in

,

by

Tags:

Comments

Leave a Reply