Tuesday, 19 May 2020

Locators for selenium | linkText locator example | anchor tag link text click example in selenium

Hi,

In this post, you will see the demonstration of "linkText" locator usage. For the purposes, I took my blog-site https://jasper-bi-suite.blogspot.com/.

linkText is one of the 8 locators supported by selenium, using it one can navigate to target page by performing click action.

For instance, navigate to "Services" page from the site.
Let's see how to perform/navigate to Services page using "Services" text from the following HTML script.
<a href="https://jasper-bi-suite.blogspot.com/p/contact-information.html">Services </a>

The following statement identifies "Services" link on the page and does the click action.
 driver.findElement(By.linkText("Services")).click();

Take a look at the complete java code at the bottom or watch below 1 min video tutorial for end-to-end execution.

Screenshot: 

Watch this 1 min video tutorial ( change quality to 720p)
linkTextDemo.java
package selenium.locators.examples;
//linkText example
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class linkTextDemo {

public static void main(String[] args) {
  
  WebDriver driver ; 
 
  System.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");
  System.setProperty("webdriver.chrome.silentOutput", "true");
  
  driver = new ChromeDriver();

  driver.navigate().to("https://jasper-bi-suite.blogspot.com/");
  driver.manage().window().maximize();
  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
  //linkText 
  driver.findElement(By.linkText("Services")).click();
  
  JavascriptExecutor js = (JavascriptExecutor) driver;
  js.executeScript("window.scrollBy(0,550)");
  
  //driver.close();

 }
}


- Sadakar Pochampalli 

No comments:

Post a Comment