How to count no. of links on webpage and print using selenium2 using java
Code for counting
no. of links on a webpage using WebDriver, TestNg , Java
package links;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class linktest {
WebDriver driver = new FirefoxDriver();
private String baseUrl="https://www.flipkart.com/";
@Test
public void testNoOfLinks() {
// Select all the links from webpage using selenium webdriver
System.out.println("Select all links from webpage using selenium webdriver");
List links = driver.findElement(By.xpath("//*")).findElements(By.tagName("a"));
System.out.println("total no. links available on webpage");
System.out.println(links.size());
System.out.println("Text of the link available on the webpage");
for(int i =0; i< links.size();i++){
System.out.println(((WebElement) list.get(i)).getText());
}
}
@BeforeTest
public void beforeTest() {
myDriver.get(baseUrl + "/");
}
package links;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class linktest {
WebDriver driver = new FirefoxDriver();
private String baseUrl="https://www.flipkart.com/";
@Test
public void testNoOfLinks() {
// Select all the links from webpage using selenium webdriver
System.out.println("Select all links from webpage using selenium webdriver");
List links = driver.findElement(By.xpath("//*")).findElements(By.tagName("a"));
System.out.println("total no. links available on webpage");
System.out.println(links.size());
System.out.println("Text of the link available on the webpage");
for(int i =0; i< links.size();i++){
System.out.println(((WebElement) list.get(i)).getText());
}
}
@BeforeTest
public void beforeTest() {
myDriver.get(baseUrl + "/");
}
Comments
Post a Comment