Locating element(s) using findElement and findElements methods

01 January, 2014
Locating an element using the findElement method Locating elements in Selenium WebDriver is done by using the findElement() and findElements() methods provided by WebDriver and WebElement class.

The findElement() method returns a WebElement object or throws an exception if it does not find any element matching the search criteria.

The findElements() method returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list.

Both the find methods take a locator or query object as an instance of By class as an argument. Selenium WebDriver provides By class to support various locator strategies. There are various locator strategies supported by Selenium WebDriver as below:

1. By ID - Locates an element using the ID attribute
    Java syntax: driver.findElement(By.id())
    Ruby syntax: driver.find_element(:id,)

2. By name - Locates an element using the Name attribute
    Java syntax: driver.findElement(By.nname())
    Ruby syntax: driver.find_element(:name,)

3. By class name - Locates an element using the Class attribute
    Java syntax: driver.findElement(By.className())
    Ruby syntax: driver.find_element(:class,)

4. By tag name - Locates an element using the HTML tag
    Java syntax: driver.findElement(By.tagName())
    Ruby syntax: driver.find_element(:tag_name,< htmltagname >)

5. By link text - Locates link using it's text
    Java syntax: driver.findElement(By.linkText())
    Ruby syntax: driver.find_element(:partial_link_text,< linktext >)

6. By partial link text - Locates link using it's partial text
    Java syntax: driver.findElement(By.partialLinkText())
    Ruby syntax: driver.find_element(:partial_link_text,< linktext >)

7. By CSS - Locates element using the CSS selector
    Java syntax: driver.findElement(By.cssSelector())
    Ruby syntax:find_element(:css,)

8. By XPath - Locates element using XPath query
    Java syntax: driver.findElement(By.xpath())
    Ruby syntax: driver.find_element(:xpath,)

No comments:

Post a Comment