How to click on each and Every Link Present on a web page and How to get the link Text?
package pkg_selenium;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class VerifyLinks {
public static WebDriver driver;
public static void main(String[] args) {
// TODO Auto-generated method stub
// Initialize WebDriver
driver = new InternetExplorerDriver();
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://newtours.demoaut.com/");
// Maximize Window
driver.manage().window().maximize();
// Verify Links
List<WebElement> listOfLinks = driver.findElements(By.tagName("a"));
String linkText[] = new String[listOfLinks.size()];
int i=0;
for(WebElement l1: listOfLinks){
// Get Link Text
linkText[i] = l1.getText();
System.out.println(linkText[i]);
i++;
}
// Click on Links
for(String t : linkText){
driver.findElement(By.linkText(t)).click();
if(driver.getTitle().contains("Under Construction")){
System.out.println(t+" : Link is under construction");
}
driver.navigate().back();
}
// Close Driver
driver.close();
}
}
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class VerifyLinks {
public static WebDriver driver;
public static void main(String[] args) {
// TODO Auto-generated method stub
// Initialize WebDriver
driver = new InternetExplorerDriver();
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://newtours.demoaut.com/");
// Maximize Window
driver.manage().window().maximize();
// Verify Links
List<WebElement> listOfLinks = driver.findElements(By.tagName("a"));
String linkText[] = new String[listOfLinks.size()];
int i=0;
for(WebElement l1: listOfLinks){
// Get Link Text
linkText[i] = l1.getText();
System.out.println(linkText[i]);
i++;
}
// Click on Links
for(String t : linkText){
driver.findElement(By.linkText(t)).click();
if(driver.getTitle().contains("Under Construction")){
System.out.println(t+" : Link is under construction");
}
driver.navigate().back();
}
// Close Driver
driver.close();
}
}
Comments
Post a Comment