Friday, September 13, 2019

Post#29.Handling Links Part-1 (link text and partial link text)


Accessing links using link text and partial link text

The easiest way to access links on a web page is using the locators’ linkText and partialLinkText. LinkText matches the entire text to find the element, whereas PartialLinkText uses the partial text to match the element.

//HTML Code
<
a href="http://www.google.com">Click Here</a>

//Selenium Code
WebElement linkTextEle= driver.
findElement(By.linkText("Click Here"));
WebElement partialEle= driver.
findElement(By.PartialLinkText("Here"));

Two things to keep in mind:

1(a). If there are more than one link texts with the same name, the above locators will identify the first occurrence only.

//HTML Code
<
a href="http://www.google.com">Click Here</a>
<
a href="http://www.bing.com">Click Here</a>
<
a href="http://www.yahoo.com">Click Here</a>

//Selenium Code
WebElement linkTextEle= driver.
findElement(By.linkText("Click Here"));
WebElement partiallinkTextEle= driver.
findElement(By.PartialLinkText("Here"));

In the above example, only the first Click Here for google.com will be identified.

1(b). The parameters for both locators are case sensitive, meaning Click Here and click here are considered two unique entities.


 //HTML Code
<a href="http://www.google.com">Click Here</a>
<
a href="http://www.bing.com">click here</a>
In the above example, we can access both the links at once, using the case-sensitivity property.

//Selenium Code
WebElement linkTextEle= driver.
findElement(By.linkText("Click Here"));
WebElement partialEle= driver.
findElement(By.PartialLinkText("here"));

No comments:

Post a Comment