Different methods to locate UI
Elements or Object Recognize Methods, Functions to locate Web Element
To locates the UI Elements or Web
Elements or to identifies the object , there are various functions or
methods to use which are as given below:-
- By ID
- By Tag Name
- By Class Name
- By Name
- By Link Text
- By XPATH
- By Partial Link Text
By ID:
This is most used and efficient method
to locate the element. All elements have its id unique always.
We can explain and understand it with
the following good example, Assume that we required to find and
locate the Google search box. Use firebug option of Mozilla Firefox
with which we can easily locate the element of the HTML format.
Following screen will help you very much:
Different methods to locate UI Elements |
< td id = “gs_tti0” class =
“gsib_a”>
id of Search box of Google is
gs_tti0
Now using ID, how can find the
element:
WebElement
element=driver.findElement(By.id(“gs_tti0”));
By Tag Name:
Using fire bug tag
name can be find
Google example
will be best for it, Assume that you have to find and locate the
'Google Search' button of Google through tag name. Use fire bug
option of the Firefox Mozila which can help you to locate the element
in HTML format easily. Following screen will help you properly:
Different methods to locate Object Recognize Methods |
<input type
= “submit” onclick = “this.checked = 1” name = “btnk”
value = “Google Search”/>
Google Search
Tag Name, Google button is input here.
Now Examples
for How to find and element with the help of Tag Name.
WebElement
element = driver.findElement(By.tagName(“input”));
By Class Name:
Multiple element
can be there which are connected with the same name of class so
searching the several element becomes the more practical options over
the searching the first element.
How element can be
found, Following example will help out:
<div class =
“tsf-p” style = “position : relative”>
Here tsf – p is
class name.
List <WebElement>
element = driver.findElements(By.className(“tsf-p”))
Always remember
that the method FindElements instead of FindElement to find several
elements.
By Link Text:
With the help
of By Link Text method of class 'By' we can search the link element
with the matching text which is visible.
Let
us execute the example of Google, Assume that we want to search or
locate the 'Advertising Programs' link of Google through the Text
Link.
Now
following example for find the element link with the help of LinkText
:-
WebElement
element = driver.findElement(By.link Text(“Advertising Program”));
By Name:
With
the help of By Name function of the class “By” we can find the
link element through name attribute of element.
Following
is the example of how to find the element which will look like the
following:
<input
type = “submit” onclick = “this.checked = 1” name = “btnk”
value = “Google Search”>
Here
“btnk” is the name.
List
<WebElement> element = driver.findElements(By.name(“btnk”))
By Partial Link Text:
With
the help of “ByPartialLinkText” function of class “By” we can
search the link element with the partial or the matching visible
text.
Let
us take the examples of Google, Assume we like to search and locate
the 'advertising programs' link of the Google through this
ByPartialLinkText , this can be done with the following:
WebElement.element=driver.findElement(By.partialLinkText(Advertising”));
By XPATH:
It is
a type of locator, Xpath. Its a unique address to identify of each
and every element. WebDriver use browser native XPath possible
capability. To search the Xpath of any elements use firebug and the
fire path ad ons on the Firefox Mozila.
Google
Example will be best, Assume that you have to search and locate the
search box of Google through Xpath. Using fire bug option of Mozila
Firefox with which we can easily locate the elements in the HTML
format and copy the Xpath from the xpath bar of the fire path tab.
Following screen will help you lot:
Functions to locate Web Element |
WebElement element = driver.findElement(By.xpath("//*[@id='gs_tti0']")); Note: Don’t copy the dot from xpath bar.
No comments:
Post a Comment