Tuesday 29 October 2013

How to find the element in selenium web driver in java?

Element identification methods are common in all languages like Java, C#.Net etc with some syntactical differences.

We can identify the web elements in selenium web driver using below methods.
  1. By Xpath
  2. By Css Selectors
  3. By Id
  4. By Name
  5. By Tag Name
  6. By Class Name
  7. By Linktext
  8. By Partial Link text
Out of these methods, xpath and css selector methods are very popular and useful. We can use xpath and css selectors to identify all kinds of elements in the webpage.

We can also identify the elements using Id, Name, Class Name provided that given element is assigned some id, name or class name.

We can also identify the elements using the html tags like table, div, form etc. 

For identifying the links, selenium web driver provides 2 additional methods like link text and Partial link text.

Web Element Identification Examples in Java - 

driver.findElement(By.tagName("TD"));
Above method will return the first element with given tag - TD.

Example -

WebElement e = driver.findElement(By.tagName("div"));
Above line will get the first element whose tag is div.

To get all div elements, you can use below code
List <WebElement> we_collection = driver.findElements(By.tagName("div"));

Please note that to get the single element we use findElement method and
to get the collection of elements we use findElements method


You can also access the element if you know its class name by using below code
driver.findElement(By.className("box"));

You can access the element if you know its id by using below code
driver.findElement(By.id("selenium"));

You can also access the element if you know the value of name attribute by using below code
driver.findElement(By.name("sagar"));

To find the links in web page, there are 2 ways as mentioned below.
driver.findElement(By.partialLinkText("news"));
driver.findElement(By.linkText("hello"));

If you know the part of the name of link, you can use By.partialLinkText
If you know the whole link name, you can use By.linkText


You can also access the element if you know its xpath expression by using below code
driver.findElement(By.xpath("//table"));

You can also access the element if you know its css expression by using below code
driver.findElement(By.cssSelector("#kid"));

To find all elements you can use below syntax.

List<WebElement> cells = tr.get(1).findElements(By.tagName("td"));
Above code will select all TD elements from the webpage.

Please note that some automation engineers use different terminologies for element identification like object identification methods or Selenium locators etc. In UFT, we use object repository to store the objects. In Selenium, we do not have separate file to store the objects or elements. But we can create our custom file which may store information about objects and their locators.
What do you think on above selenium topic. Please provide your inputs and comments. Thanks

No comments:

Post a Comment

Buy Best Selenium Books

Contributors