Friday, September 13, 2019

Post#33.Handling Links Part-5 (Clicking on each link of a web page)


Scenario: Click on first link- Navigate Back-Click on Second Link-Navigate back.. And so on..

Click on each link present on home page of http://newtours.demoaut.com/

Refer the below program to click on each link of a web page.


package pack3;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ClickOnEveryLink {

              public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:/path/geckodriver.exe");
                             FirefoxDriver driver = new FirefoxDriver();
                             driver.manage().window().maximize();
                             driver.get("https://www.google.co.in/");
                            
                             List<WebElement> links = driver.findElements(By.tagName("a"));
                            
                             for(int i=0;i<links.size();i++)
                             {
                                           if(!links.get(i).getText().isEmpty())
                                           {
                                                          System.out.println(links.get(i).getText());
                                                          links.get(i).click();
                                                          System.out.println(driver.getCurrentUrl());
                                                          driver.navigate().back();
                                                          links =driver.findElements(By.tagName("a"));
                                                         
                                           }
                                          
                             }
                             driver.quit();
              }
}

No comments:

Post a Comment