×

WebDriver-Partial Link Text locator

WebDriver-Partial Link Text locator

In selenium WebDriver, there is an additional way to find a web link element with the help of partial link text locator.

The partial link text is used to identify the web link element only, based on their part of the link text.

Partial Link text locators cannot be used for other than link elements.

The Syntax of the partial Link Text locator is as below:

driver.findElement(By.partialLinkText(“value of partial link text”)) ;

Note: The partial link text and link text both are used to identify the web link element on the web application. The difference between link text and partial link text is that the link text is used for an exact match of the text value, and partial link text is used for some portion of the long text value of the web link.

Points that have to remember:

  • Whenever multiple links are there with a similar text, then the link text and partial link text will take the help of other locators like XPath and CSS selector.
  • Both partial and link text are case sensitive.
  • Partial and link text can also be used inside and outside block of an element.

Let us see one sample test case in which we will try to locate the web link element using partial 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 website. https://www.tutorialandexample.com The Tutorial and example home page must be displayed.
3. Identify the interview question tutorial link.   An interview question 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 create a new Class 4
  • And now, we will save the class name as partial_Link_text and click on the Finish button.
class name as partial_Link_text

Now we will create our test case step by step to have an overview of how to use partial 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 the 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 move to our second step, and navigate to the desired URL,

Here the sample code:

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

Step3:

 Now we are trying to locate the interview questions web link by using the value of its partial link text attribute.

  • Right-click on the hyperlink field and select the Inspect option to identify the web link, which is as shown below.
hyperlink field and select 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:
Notice that 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 the link text locator for locating hyperlinks on the web page.
  • Copy the value of partial link text which is present in the ….. tags.

Here the sample code for the first-name hyperlink:

//identify the partial linktext
driver.findElement(By.partialLinkText("Interview")).click();
Thread.sleep(2000);
System.out.println("first name value is entered"); 

Step 4:

In the last step of our sample test case, we will close 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 Partial_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().window().maximize(); 
 //Navigate to tutorial and example home page
 driver.get("https://www.tutorialandexample.com");
 //identify the web link using partial link text 
 driver.findElement(By.partialLinkText("Interview")).click();
 Thread.sleep(2000);
 System.out.println("first name value is entered");
 //closing the browser 
 driver.close();
 }
 } 
  • To run the test script in Eclipse, right-click on the code and then select Run as ? Java Application.
To run the test script in Eclipse, right-click on the code
  • The above test script will launch the Google Chrome browser and automate all the test scenarios. 
test script will launch the Google Chrome browser and automate

 

 


Related Topics

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Selenium Assertion

Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver. An Assertion is a feature available in TestNG, which is used to verify the expected result of...

7 minutes read.

Xpath locator in Selenium IDE

X PATH LOCATOR: Xpath is used to identify the element using any attribute or visible test. Syntax for X PATH:- //html tag [@attribute=’value’] e.g. - //div [@class=’label’]                    OR //html tag [text () =’Visible Text’] e.g.-//div [text...

2 minutes 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-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.

CSS Selector Locator in Selenium IDE

It used to identify the web element based on HTML tag, id, class, and attributes. To describe the look and formatting of a document written in the markup language are known...

1 minute 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.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 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 WebDriver - Firefox or Gecko (Marionette) browser

Selenium web driver-Firefox or Gecko (Marionette) browser In this tutorial, we are going to learn how to run the Selenium WebDriver test script in the Firefox Browser using the Gecko Driver. Before going...

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.

Selenium WebDriver - IE [Internet Explorer] browser

Selenium WebDriver - IE [Internet Explorer] browser In this segment, we will understand how to run a test script with the help of the IE (Internet Explorer) browser. It is a standalone...

4 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 vs QTP

In this section, we are comparing the most frequently used automation tool –Selenium and QTP. Selenium and QTP have relative advantages over each other- FeaturesSeleniumHP QTPLicensingSelenium is an open-source testing tool....

3 minutes read.

Selenium WebDriver-CSS Selector locator

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute read.

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling In this tutorial, we will be learning about handling the checkbox in selenium WebDriver. Let us take one example to give you a better understanding of Checkbox handling,...

4 minutes 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.

Difference between Selenium WebDriver and Selenium RC

Difference between Selenium WebDriver, and Selenium RC: Selenium WebDriver and selenium RC (Remote Control) both are different in any manners which we see later in this tutorial, let us see the...

3 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.

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.