Saturday, April 28, 2012

WebElements & Locators






WebElements & Locators


In WebDriver automation everything related with web elements as it is web application automation tool.


WebElement is nothing but, all DOM objects appeared in the web page. To do operations with DOM objects/ web elements we need to locate those elements exactly.


WebElement element=driver.findElement(By.<Locator>);


As we've seen in the above statement we have to specify some locator to identify web element.
'By' is the class, in that class we have different static methods to identify elements. Those are,
  1. By.className
  2. By.cssSelector
  3. By.id
  4. By.linkText
  5. By.name
  6. By.partialLinkText
  7. By.tagName
  8. By.xpath 

1. By.className
See below example
Example:1
  <td class=name> </td>
WebElement td=driver.findElement(By.className("name"));


2.By.cssSelector
CSS selector is the one the best ways to locate some complex elements in the page.

See below some examples for easy understanding
Example:1
 <td class=name> </td>
driver.findElement(By.cssSelector("td.name"));     In css selector we can denote class name with dot (.)
                             (or)
driver.findElement(By.cssSelector("[class=name]"))    We can specify the attribute name and its value.


Example:2
<input id=create>
driver.findElement(By.cssSelector("#create")).sendKeys("test");    shortcut for denoting id is #
                                  ( or )
driver.findElement(By.cssSelector("[id=create]")).sendKeys("test")


Example:3
<td value=dynamicValue_13232><td>
driver.findElement(By.cssSelector("[value*=dynamicValue]"))     * is for checking contained value
(here value contains dynamicValue)


Example:4
          <div value=testing name=dynamic_2307></div>
driver.findElement(By.cssSelector("[value=testing][name*=dynamic]"));  


If you want to include more attribute values in locator criteria use css locator as above. 




3. By.id
See below example
Example:1
  <td id=newRecord> </td>
WebElement td=driver.findElement(By.id("newRecord"));


here we can specify the id attribute value directly.


4. By.linkText
See below example
Example:1
  <a onclick=gotonext()>Setup </a>
WebElement link=driver.findElement(By.linkText("Setup"));


This is the best locator for locating links (anchor tags) in your web page.





5. By.partialLinkText
See below example
Example:1
  <a onclick=gotonext()>very long link text </a>
WebElement link=driver.findElement(By.partialLinkText("very"));
                               (or)
WebElement link=driver.findElement(By.partialLinkText("long link"));


This is the locator for locating links (anchor tags) using partial text it contains .


6. By.name
See below example
Example:1
  <td name=WebDriver> </td>
WebElement td=driver.findElement(By.name("WebDriver"));


7. By.tagName
See below example
Example:1
  <td class=name> </td>
WebElement td=driver.findElement(By.tagName("td"));


If you want to get the entire text of web page use below logic.


driver.findElement(By.tagName("body")).getText();


8. By.xpath
              
In next post I ll post about xpath locators in detail.




Thank you.









4 comments:

  1. @Santhosh how to find a hidden div like mouseover function? is there anyway?

    ReplyDelete
    Replies
    1. @Divya :
      What do you mean by hidden div?
      Do you want to place the mouseover on that div ?
      Can you post your HTML snippet here?

      Delete
  2. how can i find whether links are available are not in diff pages.

    ReplyDelete
    Replies
    1. @Praveen : For verifying some element existence in a particular page you need write your own logic.

      See the below post for implementation of isElementPresent? logic
      http://santoshsarmajv.blogspot.in/2012/07/iselementpresent.html

      Delete

Note: Only a member of this blog may post a comment.