Software Testing Material

A site for software testers. We provide free online tutorials on Manual Testing, Automation Testing - Selenium, QTP, LoadRunner, Testing Tools and many more.

  • Blog
  • Tutorials
    • Manual Testing
    • Java
    • Selenium
      • TestNG
      • Maven
      • Jenkins
    • Framework
    • Katalon
    • Agile
    • SQL
    • VBScript
    • API Testing
  • Tools
    • TestLodge
    • FrogLogic GUI Tool
    • CrossBrowserTesting
    • BrowserStack
    • TestCaseLab
    • Kobiton
    • Sikuli
    • Postman
  • Interview Q & A
    • Selenium
    • TestNG
    • Test Framework
    • Explain Framework
    • Manual Testing
    • Software QA
    • Agile
    • Testing As A Career
    • General Interview Questions
    • API Testing
    • SOAP
  • Free Resources
  • Guest Post
    • Guest Post Guidelines
  • Training

How To Locate Element By Link Text And Partial Link Text Locators

Last Updated on February 7, 2018 by Sumasri

In the previous post we have seen “locators in Selenium“. In this post, we discuss “How To Locate Element By Link Text And Partial Link Text Locators”. Find the below links on How to find elements on a web page using different types of locators.

1. “How To Locate Element By ID Locator”
2. “How To Locate Element By Name Locator”
3. “How To Locate Element By Class Name Locator”
4. “How To Locate Element By Tag Name Locator”
5. “How To Locate Element By CSS Selector Locator”
6. “How To Locate Element By XPath Locator”

Coming to the actual post “How To Locate Element By Link Text and Partial Link Text Locators”.

Link Text Locator:

If there are multiple elements with the same link text then the first one will be selected. This Link Text locator works only on links (hyperlinks) so it is called as Link Text locator.

Syntax:

1
findElement(By<span style="color: #ff0000;">.linkText("LinkText")</span>)

Locate Element By Link Text And Partial Link Text

1
2
3
<span class="nodeLabelBox repTarget ">&lt;<span class="nodeTag ">span</span><span class="nodeAttr editGroup "> <span class="nodeName editable ">id</span>="<span class="nodeValue editable ">link-signup</span>"</span><span class="nodeBracket editable insertBefore ">&gt;
&lt;<span class="nodeTag ">a</span><span class="nodeAttr editGroup "> <span class="nodeName editable ">href</span>="<span class="nodeValue editable ">https://accounts.google.com/SignUp?service=mail&amp;continue=https%3A%2F%2Fmail.google.com%2Fmail%2F</span>"</span>&gt;<span class="nodeText editable"><span class=" "> Create account </span></span>&lt;/<span class="nodeTag ">a</span>&gt;
&lt;/span&gt;</span></span>

Value to be added in the By.linkText method:

1
findElement(By.<span style="color: #ff0000;">linkText("Create account")</span>)

Script:

1
2
3
4
5
6
7
8
9
10
11
12
13
package seleniumTutorial;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class Locators {
       public static void main (String [] args){
               WebDriver driver = new FirefoxDriver();
               driver.get("<a href="https://www.gmail.com/" target="_blank" data-saferedirecturl="https://www.google.com/url?hl=en&amp;q=https://www.gmail.com&amp;source=gmail&amp;ust=1475225889620000&amp;usg=AFQjCNGWiJEPM95P3VkSISyTSrTuSE2t5A">https://www.gmail.<wbr />com</a>");
               driver.findElement(By.linkText("Create account")).click();
      }
}

Partial Link Text:

In some situations, we may need to find links by a portion of the text in a Link Text element. it contains. In such situations, we use Partial Link Text to locate elements.

Syntax:

1
findElement(By.partialLinkText("partialLinkText"))

Locate Element By Link Text And Partial Link Text

1
2
3
4
5
<span class="nodeLabelBox repTarget ">&lt;<span class="nodeTag ">span</span><span class="nodeAttr editGroup "> <span class="nodeName editable ">id</span>="<span class="nodeValue editable ">link-signup</span>"</span><span class="nodeBracket editable insertBefore ">&gt;
&lt;<span class="nodeTag ">a</span><span class="nodeAttr editGroup "> <span class="nodeName editable ">href</span>="<span class="nodeValue editable ">https://accounts.google.com/SignUp?service=mail&amp;continue=https%3A%2F%2Fmail.google.com%2Fmail%2F</span>"</span>&gt;<span class="nodeText editable"><span class=" "> Create account </span></span>&lt;/<span class="nodeTag ">a</span>&gt;
&lt;/span&gt;
 
</span></span>

Value to be added in the By.partialLinkText method:

1
findElement(By.partialLinkText("Create"))

Script:

1
2
3
4
5
6
7
8
9
10
11
12
13
package seleniumTutorial;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class Locators {
       public static void main (String [] args){
               WebDriver driver = new FirefoxDriver();
               driver.get("https://www.gmail.com");
               driver.findElement(By.partialLinkText("Create")).click();
      }
}

SUBSCRIBE TO GET FREE EBOOK AND REGULAR UPDATES ON SOFTWARE TESTING

Filed Under: Selenium

data-matched-content-rows-num="2" data-matched-content-columns-num="3"
Previous Article:
How To Locate Element By Tag Name Locator In Selenium
Next Article:
Learn CSS Selector Selenium WebDriver Tutorial [Without Using Any Tools]

About the Author

Sumasri is a Sr. Software Test Engineer. Currently working in an MNC. She is a co-founder of Software Testing Material. She has an exclusive experience in the field of Software Testing. She writes here about Manual Testing and Automation Testing.

ADVERTISEMENT

Froglogic-Squish-GUI-Tester
CrossBrowserTesting

TUTORIALS

  • Manual Testing Tutorial
  • Selenium Tutorial
  • TestNG Tutorial
  • VBScript Tutorial
  • Agile Methodology
  • SQL Tutorial for Testers
  • INTERVIEW QUESTIONS

  • Framework Interview Questions
  • Real Time Manual Testing
  • 100 Top Selenium Interview Questions
  • 30 Top TestNG Interview Questions
  • Agile Interview Questions
  • 80 SQL Interview Questions
  • TESTING TOOLS

  • Test Lodge
  • FrogLogic Squish
  • Cross Browser Testing
  • BrowserStack
  • Test Case Lab
  • Sikuli
  • © 2019 www.softwaretestingmaterial.com | About us | Privacy Policy | Contact us | Guest Post | Disclaimer | Terms of use | Sitemap