Posts Tagged ‘Webdriver’

How to extract no. of links and text of  links present on a page in Selenium WebDriver:

We all know that tag name for every link starts with letter “a”. “a” refers to the anchor tag. The tag name which starts with letter “a” should be a link.

Because all links has tag name “a” so we can identify links with tagName method of By class i.e.

driver.findElements(By.tagName(“a”));

To find the total no of links on a page:

Suppose you want to find total no. of links on google page for this you can use

driver.get(“http://www.google.com”);

List<WebElement> alllinkspresent=driver.findElements(By.tagName(“a”));

System.out.println(“no. of links on google page are :” +alllinkspresent.size());

Where size() method returns the total no of links on Google page.

To find or print the name of links i.e. text of links:

Suppose you want to print text of all links present on Google page for this you can use

for(int i=0; i< alllinkspresent.size(); i++)

{

System.out.println(alllinkspresent.get(i).getText());

}

To extract the links of a particular section:

Suppose you want to extract the links of languages offered by Google section for this you can use

WebElement LangSection=driver.findElement(By.xpath (“//*[@id=’addlang’]”));

List<WebElement> lang=LangSection.findElements(By.tagName(“a”));

for(int i=0; i< lang.size(); i++)

{

System.out.println(lang.get(i).getText());

}

Where LangSection is one WebElement and we have to find the all the links present in this section. For understanding of this section of code please refer following screen shot

When you run the above code you will get text of all links present in this section. Refer following screen shot

Different methods to locate UI Elements (WebElements) or Object Recognize Methods:

There are different methods to locate UI Elements (WebElements) or to recognize objects which are as follows:
· By ID
· By Tag Name
· By Class Name
· By Link Text
· By Name
· By Partial Link Text
· By XPATH

By ID:

This method is most efficient and mostly used to locate the elements. Every element has its unique id.

Let’s take the example for Google, suppose we have to find/locate the search box of goggle. Use firebug option of Mozilla Firefox with which you can easily locate the element in HTML format. Please refer following screenshot:

<td id=”gs_tti0″ class=”gsib_a”>

Id of Search box of goggle is gs_tti0.

Now Example for how to find an element via using ID:

WebElement element = driver.findElement(By.id(“gs_tti0”));

 By Tag Name:

To find tag name of a use fire bug.

Let’s take the example for Google, suppose we have to find/locate the “Google Search” button of goggle via tag name. Use firebug option of Mozilla Firefox with which you can easily locate the element in HTML format. Please refer following screenshot:

<input type=”submit” onclick=”this.checked=1″ name=”btnK” value=”Google Search”/>

Tag Name of Google Search” button of goggle is input here.

Now Example for how to find an element via using TagName:

WebElement element = driver.findElement(By.tagName("input"));

By Class Name:

There may be many elements that are associated with same class name so finding multiple elements becomes the more practical option over finding the first element.

Example of how to find an element that looks like this:

<div class=”tsf-p” style=”position:relative”>

Here tsf-p is classname.

List<WebElement> element = driver.findElements(By.className("tsf-p "))
Remember that use findElements method instead of findElement to find multiple elements.

 By Link Text:

By using By Link Text method of class “By “you can find link element with matching visible text.

Let’s take the example for Google, suppose you want to find/locate the “Advertising Programs” link of goggle via Link Text.

Now Example for how to find an element i.e. link via using linkText:

WebElement element = driver.findElement(By.linkText("Advertising Programs"));

By Name:

By using By Name method of class “By “you can find link element via name attribute of element.

Example of how to find an element that looks like this:

<input type=”submit” onclick=”this.checked=1″ name=”btnK” value=”Google Search”>

Here” btnK” is name.

List<WebElement> element = driver.findElements(By.name("btnK "))

By Partial Link Text:

By using “BypartialLinkText” method of class “By “you can find link element with Partial matching visible text.

Let’s take the example for Google, suppose you want to find/locate the “Advertising Programs” link of goggle via BypartialLinkText. For this you can use:

WebElement element = driver.findElement(By. partialLinkText ("Advertising"));

 By XPATH:

Xpath is a locator, it is an unique address which identifies each and every element.WebDriver uses a browser’s native XPath capabilities wherever possible. To find xpath of an element use firebug and fire path adons on Mozilla Firefox.

Let’s take the example for Google, suppose we have to find/locate the search box of goggle via using xpath. Use firebug option of Mozilla Firefox with which you can easily locate the element in HTML format and copy the xpath from xpath bar of firepath tab.Please refer following screenshot:

Now Example for how to find an element via using xpath:

WebElement element = driver.findElement(By.xpath("//*[@id='gs_tti0']"));
 Note: Don’t copy the dot from xpath bar.

 

Selenium logo

Selenium2/Selenium Webdriver:

Selenium2=Selenium1+WebDriver

Webdriver is a tool for automating testing web applications. Webdriver makes direct calls to the browser using each browser’s native support for automation. Selenium Webdriver does not depends upon the server. We can directly run the programs/scripts in the main method. Selenium Webdriver is very simple, easy to use and to the point.

Features of Selenium Webdriver:

  • Selenium Webdriver supports various languages for example java, Python, C#, Ruby, Perl, Php, Java script.
  • No need to start selenium server.
  • Selenium Webdriver Supports iPhone and Android testing.
  • You can find coordinates of any object using Webdriver.
  • You can easily simulate clicking on front and back button of browser.
  • Keyword driven framework is very easy to build in webdriver.
  • Webdriver uses native automation and does not have the sandbox constraints of Selenium-RC. It’s a little faster and does not require a server.

 

If you like this post ,Don’t forget to write comments…………………