Friday, September 13, 2019

Post#30.Handling Links Part-2 (Finding All links on a web page)


Finding links on a web page

1) Navigate to the interested webpage for e.g. http://newtours.demoaut.com/
2) Create a list of type WebElement to store all the Link elements in to it.
3) Collect all the links from the webpage. All the links are associated with the Tag a‘.
4) Now iterate through every link and print the Link Text on the console screen.


package pack4;
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;
public class captureAllLinks {

              public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:/path/geckodriver.exe");
                             WebDriver driver = new FirefoxDriver();
                             driver.manage().window().maximize();

                             driver.get("http://newtours.demoaut.com/");

                             List<WebElement> links = driver.findElements(By.tagName("a"));

                             System.out.println(links.size());

                             for (int i = 1; i<=links.size(); i=i+1)

                             {
                                           System.out.println(links.get(i).getText());
                             }
                             driver.quit();
              }
}

No comments:

Post a Comment