0% found this document useful (0 votes)
93 views

Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

fghfghfgh

Uploaded by

Interact people
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

fghfghfgh

Uploaded by

Interact people
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

Locate Elements by Link Text & Par al Link Text in


Selenium Webdriver
Links Matching a Criterion

Links can be accessed using an exact or partial match of their link text. The examples below
provide scenarios where multiple matches would exist and would explain how WebDriver
would deal with them.

In this tutorial, we will learn the available methods to find and access the Links using
Webdriver. Also, we will discuss some of the common problems faced while accessing Links
and will further discuss on how to resolve them.

Here is what you will learn-

Accessing links using Exact Text Match: By.linkText()


Accessing links using Partial Text Match: By.partialLinkText()
How to get Multiple links with the same Link Text
Case-sensitivity for Link Text
Links Outside and Inside a Block

Accessing links using Exact Text Match: By.linkText()


Accessing links using their exact link text is done through the By.linkText() method.
However, if there are two links that have the very same link text, this method will only access
the first one. Consider the HTML code below

(/images/image002.png)

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 1/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

(/images/image003(1).png)

When you try to run the WebDriver code below, you will be accessing the first "click here"
link

(/images/image004(1).png)

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyClass {

public static void main(String[] args) {

String baseUrl = "http://demo.guru99.com/test/link.html";

System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get(baseUrl);
driver.findElement(By.linkText("click here")).click();

System.out.println("title of page is: " + driver.getTitle());

driver.quit();
}

Here is how it works-

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 2/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

(/images/2-

2017/080817_0703_AccessingLi4.png)

As a result, you will automatically be taken to Google.

(/images/image005(1).png)

Accessing links using Par al Text Match: By.par alLinkText()


Accessing links using a portion of their link text is done using the By.partialLinkText()
method. If you specify a partial link text that has multiple matches, only the first match will be
accessed. Consider the HTML code below.

(/images/image006(1).png)

(/images/image007(1).png)

When you execute the WebDriver code below, you will still be taken to Google.

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 3/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

(/images/image008(1).png)

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class P1 {

public static void main(String[] args) {

String baseUrl = "http://demo.guru99.com/test/accessing-link.html";

System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get(baseUrl);
driver.findElement(By.partialLinkText("here")).click();

System.out.println("Title of page is: " + driver.getTitle());

driver.quit();
}
}

(/images/image009(1).png)

How to get Mul ple links with the same Link Text
So, how to get around the above problem? In cases where there are multiple links with the
same link text, and we want to access the links other than the first one, how do we go about
it?

In such cases, generally, different locators viz... By.xpath(), By.cssSelector() or By.tagName()


are used.

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 4/11
Most commonly used is By.xpath().
12/13/2018 It is the
Locate Elements most
by Link reliable
Text & Partial Linkone
Text inbut it looks
Selenium complex and non-
Webdriver

readable too.

Case-sensi vity for Link Text

(/images/2-

2017/080817_0703_AccessingLi10.png)

The parameters for By.linkText() and By.partialLinkText() are both case-sensitive, meaning
that capitalization matters. For example, in Mercury Tours' homepage, there are two links
that contain the text "egis" - one is the "REGISTER" link found at the top menu, and the
other is the "Register here" link found at the lower right portion of the page.

(/images/image010(1).png)

Though both links contain the character sequence "egis," one is the "By.partialLinkText()"
method will access these two links separately depending on the capitalization of the
characters. See the sample code below.

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 5/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

(/images/image011(1).png)

Code

public static void main(String[] args) {

String baseUrl = "http://demo.guru99.com/test/newtours/";

System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get(baseUrl);
String theLinkText = driver.findElement(By
.partialLinkText("egis"))
.getText();
System.out.println(theLinkText);
theLinkText = driver.findElement(By
.partialLinkText("EGIS"))
.getText();
System.out.println(theLinkText);

driver.quit();

Links Outside and Inside a Block


The latest HTML5 standard allows the <a> tags to be placed inside and outside of block-
level tags like <div>, <p>, or <h3>. The "By.linkText()" and "By.partialLinkText()" methods can
access a link located outside and inside these block-level elements. Consider the HTML
code below.

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 6/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

(/images/image014(1).png)

(/images/image015(1).png)

The WebDriver code below accesses both of these links using By.partialLinkText() method.

(/images/image016(1).png)

Code:

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 7/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyClass {

public static void main(String[] args) {

String baseUrl = "http://demo.guru99.com/test/block.html";

System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get(baseUrl);
driver.findElement(By.partialLinkText("Inside")).click();

System.out.println(driver.getTitle());
driver.navigate().back();
driver.findElement(By.partialLinkText("Outside")).click();

System.out.println(driver.getTitle());
driver.quit();
}
}

The output above confirms that both links were accessed successfully because their
respective page titles were retrieved correctly.

Summary

Links are accessed using the click() method.


Apart from the locators available for any WebElement, Links also have link text based
locators:
By.linkText() – locates the links based on the exact match of the link's text provided as
a parameter.
By.partialLinkText() – locates links based on the partial text match of the link's text.
Both the above locators are case Sensitive.
If there are multiple matches, By.linkText() and By.partialLinkText() will only select the first
match. In such cases where multiple links with the same link text are present, other
locators based on xpath, CSS are used.
findElements() & By.tagName("a") method finds all the elements in the page matching the
locator criteria
Links can be accessed by the By.linkText() and By.partialLinkText() whether they are
inside or outside block-level elements.

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 8/11
 Prev (/select-option-dropdown-selenium-webdriver.html)
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
Report a Bug

Next  (/keyboard-mouse-events-files-webdriver.html)

YOU MIGHT LIKE:

SELENIUM SELENIUM SELENIUM

(/verify-tooltip-selenium- (/xslt-report- (/top-100-selenium-


webdriver.html) selenium.html) interview-questions-
(/verify-tooltip- (/xslt-report- answers.html)
selenium- selenium.html) (/top-100-selenium-
webdriver.html) XSLT Report in Selenium interview-questions-
How to Verify Tool p using answers.html)
(/xslt-report-selenium.html)
Selenium WebDriver Top 100 Selenium Interview
(/verify-tooltip-selenium- Ques ons & Answers
webdriver.html) (/top-100-selenium-
interview-questions-
answers.html)

SELENIUM SELENIUM SELENIUM

(/handling-ajax-call- (/find-element- (/sikuli-tutorial.html)


selenium-webdriver.html) selenium.html) (/sikuli-tutorial.html)
(/handling-ajax-call- (/find-element-
File Upload using Sikuli in
selenium- selenium.html) Selenium Webdriver
webdriver.html) Find Element and (/sikuli-tutorial.html)
Handling AJAX Call in FindElements in Selenium
Selenium Webdriver WebDriver
(/handling-ajax-call- (/find-element-
selenium-webdriver.html) selenium.html)

Selenium Tutorials
1) Introduction (/introduction-to-selenium.html)

2) Intro WebDriver (/introduction-webdriver-comparison-selenium-rc.html)

3) Install Webdriver (/installing-selenium-webdriver.html)

4) First WebDriver Script (/first-webdriver-script.html)

5) Locators (/locators-in-selenium-ide.html)

6) Find Element Selenium (/find-element-selenium.html)

7) Forms & Webdriver (/accessing-forms-in-webdriver.html)


https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 9/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
8) CheckBox & Radio Button WebDriver (/checkbox-and-radio-button-webdriver.html)

9) Click Image Webdriver (/click-on-image-in-selenium.html)

10) Selenium Webdriver DropDown (/select-option-dropdown-selenium-webdriver.html)

11) Links & Tables (/accessing-links-tables-selenium-webdriver.html)

12) Keyboard Mouse Events (/keyboard-mouse-events-files-webdriver.html)

13) Upload & Download File (/upload-download-file-selenium-webdriver.html)

14) XPath in Selenium (/xpath-selenium.html)

15) Alert & Popup handling (/alert-popup-handling-selenium.html)

16) Handle Web Table (/selenium-webtable.html)

17) Handling Dynamic Web Tables (/handling-dynamic-selenium-webdriver.html)

18) Desired Capabilities in Selenium (/desired-capabilities-selenium.html)

19) Verify Tooltip WebDriver (/verify-tooltip-selenium-webdriver.html)

20) Find Broken links (/find-broken-links-selenium-webdriver.html)

21) Gecko (Marionette) Driver (/gecko-marionette-driver-selenium.html)

22) Install TestNG in Eclipse (/install-testng-in-eclipse.html)

23) Selenium & TestNG (/all-about-testng-and-selenium.html)

24) Introduction to TestNG Groups (/introduction-testng-groups.html)

25) Test Case Priority in TestNG (/test-case-priority-testng.html)

2) Install IDE & FireBug (/install-selenuim-ide.html)

3) Introduction IDE (/introduction-selenuim-ide.html)

4) First Script (/first-selenium-test-script.html)

6) Enhancements (/enhancing-selenium-ide-script.html)

7) Variables, Echo, Alert, PopUp (/store-variables-handling-selenium-ide.html)

 (https://www.facebook.com/guru99com/) 
(https://twitter.com/guru99com) 
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)

(https://forms.aweber.com/form/46/724807646.htm)
https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 10/11
12/13/2018 Locate Elements by Link Text & Partial Link Text in Selenium Webdriver

About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)

Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)

Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)

Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)

© Copyright - Guru99 2018


Privacy Policy (/privacy-policy.html)

https://www.guru99.com/accessing-links-tables-selenium-webdriver.html 11/11

You might also like