Pages

5/18/2016

locating webelements using selenium


Record and playback is a Novice routine and I am not interested in it.

 Selenium provides several options to locate Web Elements such as (h1,p,div...) .

The attributes id,name,classname are used for identifying particular element inside html for example

<html>
 <body>
    <p id="para1">Hi</p>
    <input type=textarea name="text" >
    <p class="para2">Hello</p>
    <p class="para2">World</p>
    <a id="linkone">link1</a>
 </body>
<html>

#para1 {
  color: blue;
}
.para2 {
 color: green;
}

we are giving id to <p id="para1">  for identify the element in order to change the property of that element. if you are using css or javascript id cannot be repeated unlike class="para2" , class name can be repeated and name is the attribute of input . In selenium there are classes and methods to get these elements by passing this id,name and classname 

In selenium we can write it as :

for id : WebDriver.findElement(By.id("para1"))    , this will return a particular element.

for class : WebDriver.findElement(By.className("para2"))   , this will return all the elements which has the classname which would be a array of elements.

for nameWebDriver.findElement(By.name("text"))  , this will return the particular element .

for css : WebDriver.findElement(By.cssSelector("#para1 or . para2"))      ,  if you are giving #para1 it will return single element as like id , if you are giving .para2 this will return 2 element which is an array of elements.

for link : WebDriver.findElement(By.linkText(""))   , this will return the link which contains the text "link1"

for tagname : WebDriver.findElement(By.tagName("p"))   , this will return all the paragraph tag elements.

for patialLink : WebDriver.findElement(By.partialLinkText("link1"))  , this is same as the link but the difference is text contains a partial link.
   
for  xpath : WebDriver.findElement(By.xpath("/html/body/p"))    , this will return a element or array of elements . i will explain xpath in more in coming post.
 

No comments:

Post a Comment

widget

installing perl dancer in ubuntu

For installing perl dancer you need curl utility for installing curl ,   sudo apt-get install curl Now we can install Perl-Dancer by thi...