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.
//Selenium Code
WebElement linkTextEle= driver.findElement(By.linkText("Click Here"));
WebElement partialEle= driver.findElement(By.PartialLinkText("Here"));
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>
<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"));
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.
<a href="http://www.google.com">Click
Here</a>
<a href="http://www.bing.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"));
WebElement linkTextEle= driver.findElement(By.linkText("Click Here"));
WebElement partialEle= driver.findElement(By.PartialLinkText("here"));
No comments:
Post a Comment