Site icon

Validate particular link on Web Page using Selenium Web Driver (Java)

Code: Validate the particular link on the Web Page using Selenium WebDriver

package org.learn;

import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Random {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C:\\\\work\\\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https:www.gmail.com");
		// Give count of number of links on page
		int numberOfLinks = driver.findElements(By.tagName("a")).size();
		System.out.println("Number of links on Web Page :" + numberOfLinks);

		// Click on all the links to open them in a new tab
		for (int i = 0; i < numberOfLinks; i++) {
			String clickOnClick = Keys.chord(Keys.CONTROL, Keys.ENTER);
			driver.findElements(By.tagName("a")).get(i).sendKeys(clickOnClick);
		}
		
		//To validate the url or title of particular link
		Set ids = driver.getWindowHandles();
		Iterator<String> it = ids.iterator();
		while (it.hasNext()) {
			driver.switchTo().window(it.next());
			System.out.println(driver.getTitle());
			//System.out.println(driver.getCurrentUrl());
		}

	}
}

Output – Validate the title/url of particular link on the Web Page using Selenium

Gmail
Google Terms of Service – Privacy & Terms – Google
Privacy Policy – Privacy & Terms – Google
Google Account Help
Browse Chrome as a guest - Computer - Google Chrome Help
Exit mobile version