×

WebDriver-Link Text locator

WebDriver-Link Text locator

The Link text locator is used to identify only web link elements based on the value of the link.

The Link text locators cannot be used for other than the link element.

The Syntax of the Link Text locator is as below:

driver.findElement(By.linkText(“value of the link text attribute”));

Note: Whenever multiple links are there, the link text will give preference to the first one.

Let us see one sample test case in which we will try to inspect the web element with the help of link text locators and automate the following scenarios:

Steps Actions Input Expected Result
1. Open the Google chrome browser.   The Google chrome browser should be opened.
2. Navigate to the tutorial and example. https://www.tutorialandexample.com Tutorial and example home page must be displayed.
3. Identify the angular 8 tutorial link.   An angular 8 tutorial page must be opened.
4. Close the browser.   The Browser should be closed.
  • After launching the Eclipse, we will open the existing test suite new_test, which we have created in earlier sessions of the WebDriver tutorial.
  • And then, right-click on the src folder and create a new Class File from New ? Class.
right-click on the src folder
  • And now, we will save the class as Link_text and click on the Finish button.
 we will save the class as Link_text

Now we will create our test case step by step to have an overview of how to use link text locators to identify a particular web element.

Step1:

  • To access the Google Chrome browser first, we need to download the Google Chrome driver and set the system property for Chrome driver.
  • We have already discussed this in previous sessions of the tutorial.
  • And you can also refer given link "Working with Chrome browser” for a better understanding that how we can download it and set System property for Chrome driver.
// set the system property for chrome browser
 System.setProperty("webdriver.chrome.driver",
"C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
 //create the object for chrome driver
 WebDriver driver = new ChromeDriver();

Step2:

After that, we will navigate to the given URL.

Here the sample code,

//navigate to the URL
driver.get("https://www.tutorialandexample.com");

Step3:

 Now we are trying to locate the angular 8 tutorial web link by using the value of its link attribute.

  • Right-click on the hyperlink field and select the Inspect option to identify the web link which is as shown below.
the Inspect option to identify the web link
  • Notice that the Html code belongs to the hyperlink got highlighted, which is as shown in the below snapshot:
the Html code belongs to the hyperlink got highlighted
  • And notice that the above highlighted Html code has a text between the to tags.
  • We are using this text value for link text locator for locating hyperlinks on the web page.
  • Copy the value of the link text which is present in the ….. tags.

Here the sample code for the first-name hyperlink:

//identify the link
driver.findElement(By.linkText("Angular 8 Tutorial")).click();
Thread.sleep(2000);
System.out.println("first name value is entered"); 

Step 4:

In the last step of our sample test case, we are closing the Chrome browser,

The sample code for closing the browser,

//Closing the browser
 driver.close();

 Our final test script will look like this:

package testpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Link_text {
public static void main(String[] args) throws InterruptedException {
//set the system property
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
 //creating the object for Chrome driver
 WebDriver driver = new ChromeDriver();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 driver.manage().window().maximize();
 //Navigate to tutorial and example home page
 driver.get("https://www.tutorialandexample.com");                                        
//identify the link
 driver.findElement(By.linkText("Angular 8 Tutorial")).click();
 Thread.sleep(2000);
 System.out.println("first name value is entered");
 //closing the browser 
 driver.close();
 }
 } 
  • To run the code in Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google chrome browser and automate all the test scenarios. 
script will launch the Google chrome browser and automate

 

 


Related Topics

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 minutes read.

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

Selenium WebDriver-XPath Locator

Selenium WebDriver-XPath Locator The locators can make the object identification of all the selenium tools. There are eight locators available in selenium web driver, but most of the time we go for...

14 minutes read.

WebDriver Interface | Class Diagram

WebDriver interface/class diagram: The interface of WebDriver includes the following stages, which is as follows: Search Context Web driver Remote web driver Driver class Search Context  The first interface of the web driver class is search Context,...

2 minutes read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

14 minutes read.

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

4 minutes read.

WebDriver-Id Locator

WebDriver-Id Locator: The Id Locator is used to identify any web element in UI with the help of the ID attribute. The First preference is always goes to ID locator while inspecting...

4 minutes read.

Selenium WebDriver - Google Chrome Browser

Selenium WebDriver - Google Chrome Browser In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser. To implement the WebDriver protocol with the help...

3 minutes read.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

Selenium WebDriver-CSS Selector: Tag and ID

Selenium WebDriver-CSS Selector: Tag and ID We will locate the web element with the help of tag and id CSS selector in this section of the tutorials Where Tag and id are...

4 minutes read.

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

4 minutes read.

Link Text Locator in Selenium IDE

LINK TEXT Link textlocator is used to identify only the weblink element based on the name of the link. Whenever multiple links are there, it will not work; it will work only...

1 minute read.

Selenium WebDriver-CSS Selector: Tag Class and Attribute

Selenium WebDriver-CSS Selector: Tag, Class, and Attribute We will locate the web element with the help of Tag, class, and attribute CSS selector locator in this section of the tutorial. Where Tag,...

5 minutes read.

WebDriver-Class Name Locator

WebDriver-Class Name Locator In this tutorial, we will learn about the Class name locator of selenium WebDriver. The Class Name locator is used to identify any element in UI based on the...

4 minutes read.

Drawback or Limitations of Selenium

Drawback or Limitations of Selenium Followings are the Drawback or limitations of Selenium:- Drawback /LimitationsDescriptionDesktop applications cannot be automated by Selenium.To test the desktop application, we have tools like Test Complete, Winnium, and...

2 minutes read.