What you will Learn in this blog:
- linkText() method usage in selenium
- partialLinkText() method usage in selenium
- Source code
linkText() method usage in selenium
Let us now see the usage of ‘linkText()’ method in selenium.
Launch https://www.bbc.com/
There are many links on the home page like News, Sport etc as can be seen below
Let us inspect any link.
Notice below that the link is represented by tag ‘a’
We can easily click this link by using linkText() method, see below. This method uses an exact match against the text present on website (in this example, the exact text should be ‘News’)
When you run this script, notice that the ‘News’ link gets clicked and the ‘News’ page opens as shown below
Let us now click ‘Sport’ link
Save and execute, the sports page comes up
Let us now change ‘Sport’ to ‘sport’ and see whether sport link will be clicked or not
Execute, notice below we get ‘NoSuchElementException’
This means that, the text is case sensitive and hence we should write the exact text that is seen on website.
PartialLinkText() method usage in selenium
Launch https://www.bbc.com/
Right click and inspect any news. As an example, the text of the below news is quite huge ‘Record surge in days over key 1.5C warming limit’. In such cases, it is not a good idea to use linkText(), because, in that case, we have to write the entire text for the exact match
Instead, we can use ‘partialLinkText()’ method. The advantage with this method is that, we can use only a partial text instead of whole text. So if you see the above figure, we can use partial text, for example, ‘over key’ to find the link and click it
When you run this script, notice below that the link is clicked and the news page opens
Next, if we change ‘over key’ to ‘Over key’ and run the script, we will get the ‘NoSuchElementException’ as expected. The text is case-sensitive
Let us see another example, launch https://www.udemy.com/course/selenium-real-time-examplesinterview-questions/
Inspect the link ‘Rahul Shetty’
Let us use the partial text of this link ‘Shetty’ to click the link
Execute the script.
Notice that the link is clicked and the page scrolls down
So this is how we can use linkText() and partialLinkText() methods to click the links on a webpage.
Source code
package Intro;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SeleniumScript {
public static void main(String[] args) {
WebDriverManager.firefoxdriver().setup();
WebDriver driver = new FirefoxDriver();
//linkText method
driver.get("https://www.bbc.com/");
driver.findElement(By.linkText("News")).click();
driver.navigate().back();
driver.findElement(By.linkText("Sport")).click();
driver.navigate().back();
//partialLinkText method
driver.findElement(By.partialLinkText("over key")).click();
driver.get("https://www.udemy.com/course/selenium-real-time-examplesinterview-questions/");
driver.findElement(By.partialLinkText("Shetty")).click();
}
}
Thank you for reading!