Wednesday 20 July 2016

Selenium2: WebDriver: Locate element by By Link Text Or Partial Link Text

WebDriver interface provides number of methods to locate elements in a web page. You can locate an element by using class name, id, name, linktext etc., As the name suggest linktext, partialLinkText methods used to locate hyper links. In this post, I am going to explain how to locate an element using link text (or) partial link text.

Locate Element By Link Text
Following is a hyper link, “Java Tutorial” (Which is in between <a> tags) is usually called as link text.

<a href="https://self-learning-java-tutorial.blogspot.com">Java Tutorial</a>

Suppose you want to click above element using link text, you can use following statement.

WebElement element = driver.findElement(By.linkText("Java Tutorial"));

Let us assume following index.jsp file is running at url “http://localhost:8080/application/”.


index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<body>
 <div class="center">
  <h1>Simple application to demonstrate Selenium webdriver</h1>
 </div>

 <div class="center">
  <p id="para1">WebDriver interface provides number of methods to
   locate elements in a web page. You can locate an element by using
   class name, id, name, linktext etc.,</p>
   
  <a href="https://self-learning-java-tutorial.blogspot.com">Java Tutorial</a>
  <a href="https://wiki.haskell.org/Web/Frameworks">Haskell Home</a>
  <a href="http://julialang.org">Julia Tutorial</a>
 </div>
</body>
</html>


Following application open the href link url associated with link text "Java Tutorial".

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class App {
 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://localhost:8080/application");

  System.out.println("Data for link text \"Java Tutorial\" is ");
  System.out.println("------------------------------");

  WebElement element = driver.findElement(By.linkText("Java Tutorial"));
  element.click();
 }
}

Locate element by Partial Link Text
It is used when you are not sure about the exact link text. By using patialLinkText method, you can supply substring of linktext, It identifies all the elements that matches given  text partially. As you observe, index.jsp, it has following hyper links.

<a href="https://self-learning-java-tutorial.blogspot.com">Java Tutorial</a>
<a href="https://wiki.haskell.org/Web/Frameworks">Haskell Home</a>
<a href="http://julialang.org">Julia Tutorial</a>

Following statement matches the elements with link text "Java Tutorial", "Julia Tutorial". It is because, partialLinkText "Tutorial" is a substring in both "Java Tutorial", "Julia Tutorial".

List<WebElement> elements = driver.findElements(By.partialLinkText("Tutorial"));

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class App {
 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://localhost:8080/application");

  System.out.println("Data for partial link text \"Tutorial\" is ");
  System.out.println("------------------------------");

  List<WebElement> elements = driver.findElements(By
    .partialLinkText("Tutorial"));

  for (WebElement element : elements){
   System.out.println(element.getText());
  }
   
 }
}


Output

Data for partial link text "Tutorial" is 
------------------------------
Java Tutorial
Julia Tutorial




Previous                                                 Next                                                 Home

No comments:

Post a Comment