Tuesday 8 July 2014

How to store Text of the element in selenium wedriver (using getText())



-> We can use getText() method used to display only the text with in the tag.

-> Its display all text contains with in any tag or element. Its wont display anything if there is not text available.


Example 1:

<a id="SHO">
<span lang="VIDEOS">arunrajvdm</span>
</a>

o/p (web page) arunrajvdm (link)

String ans=driver1.findElement(By.linkText("arunrajvdm")).getText()
System.out.println("Get Videos="+ans);


o/p (Selenium) : arunrajvdm (text)




Example 2:

<li id="li25">
 <b class="font-10">
  <span lang="LBLSKU">This is</span>
  :
 </b>
  official blogger
</li>


o/p (web Page)  This is official blogger


String ans=driver1.findElement(By.id("li25")).getText()
System.out.println("Get Videos="+ans);


o/p (Selenium) : This is official blogger (text)

Its display all the text message with in the <li> tag. So getText() used to display conatins all text with in the tag



Example 3:


<select name="SelectNUmber" class="form-drop">
<option selected="selected" title="1">1</option>
<option  title="2">2</option>
<option  title="3">3</option>
<option  title="4">4</option>
<option  title="6">6</option>

</select>


Code:

Str1=driver1.findElement(By.xpath("//td[3]/select")).getText();
System.out.println("All the TExt ="+Str1);

O/p
All the TExt = 1
2
3
4
6



Example 4:

<select name="SelectNUmber" class="form-drop">
<option selected="selected" title="1">1</option>
<option  title="2">2</option>
<option  title="3">3</option>
<option  title="4">4</option>
<option  title="6">6</option>

</select>


Code:

Str1=driver1.findElement(By.tagName("select")).getText();
System.out.println("All the TExt ="+Str1);


O/p
All the TExt = 1
2
3
4
6

No comments:

Post a Comment