Locate a link and click on it

In this section, we will learn how to locate a link and perform click operation on it. We will be using 'linkText' and 'partialLinkText' locators to locate the links.

Please see the following screenshot showing the links where we are performing the operations.




Please see the following code to perform this operation.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

 public static void main(String[] args) {

  System.setProperty("webdriver.gecko.driver", "./MyProject/driver/geckodriver.exe");
  WebDriver driver = new FirefoxDriver();

  driver.get("https://selenium-vinod.blogspot.in/p/selenium-webdriver-architecture.html");

  // We are locating a link "Next" to navigate to next page using a locator
  // LinkText
  driver.findElement(By.linkText("Next")).click();

  // We are locating a link "Previous" to navigate to previous page using a
  // locator PartialLinkText
  driver.findElement(By.partialLinkText("05_Practice")).click();

  driver.close();
 }
}

                                          <-- Previous || Next -->

No comments:

Post a Comment