Friday 8 May 2015

Selenium:How to find the all links in selenium web driver

package New;

import java.util.Date;
import java.util.Iterator;
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.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class LinksStore {

    public WebDriver driver;

    String s1;
   
     @BeforeClass
     public void openBrowser() throws Exception {
     driver= new FirefoxDriver();
     driver.manage().window().maximize();
     }
     @AfterClass
     public void closeBrowser() throws Exception {
     driver.quit();
     }
     @Test
     public void linksVerifification() throws InterruptedException{
         driver.get("http://www.bing.com/");
         /*List<WebElement> ele=driver.findElements(By.tagName("a"));
         Iterator<WebElement> s=ele.iterator();
         while (s.hasNext()) {
            WebElement e=s.next();
            if(e!=null){
                 s1=e.getText();
                System.out.println("active link is:"+s1);
            }
            else{
                System.out.println("non active links are:"+s1);
            }
           
       
           
        }*/
       
         List<WebElement> ele=driver.findElements(By.tagName("a"));
         for (int i = 0; i < ele.size(); i++) {
             if(ele.get(i)!=null){
               
                 System.out.println(ele.get(i).getText());
                 if(ele.get(i).getText().equalsIgnoreCase("NEWS")){
                    /* driver.findElement(By.xpath("//*[@id='scpt4']/a")).click();
                     Thread.sleep(5000);*/
                     driver.findElement(By.name(ele.get(i).getText())).click();
                 }
                 System.out.println("active link is:"+ele.get(i));
             }
             else{
                 System.out.println(ele.get(i).getText());
                 System.out.println("non active link is:"+ele.get(i));
             }
           
           
        }
     }
   
}

No comments:

Post a Comment