I have 10 different pages contain different links. How to click on all the links?
Conditions are : i) I don\'t know how many links are there ii) I want to count and
public static void main(String[] args)
{
FirefoxDriver fd=new FirefoxDriver();
fd.get("http:www.facebook.com");
List<WebElement> links=fd.findElements(By.tagName("a"));
System.out.println("no of links:" +links.size());
for(int i=0;i<links.size();i++)
{
if(!(links.get(i).getText().isEmpty()))
{
links.get(i).click();
fd.navigate().back();
links=fd.findElements(By.tagName("a"));
}
}
}
This program clicks on a link, navigates back to the page and clicks the second link again.
package selenium.tests;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestAllLinks {
public static void main(String[] args) {
String baseUrl = "http://www.qaautomated.com/";
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
String notWorkingUrlTitle = "Under Construction: QAAutomated";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
String[] linkTexts = new String[linkElements.size()];
int i = 0;
//extract the link texts of each link element
for (WebElement elements : linkElements) {
linkTexts[i] = elements.getText();
i++;
}
//test each link
for (String t : linkTexts) {
driver.findElement(By.linkText(t)).click();
if (driver.getTitle().equals(notWorkingUrlTitle )) {
System.out.println("\"" + t + "\""
+ " is not working.");
} else {
System.out.println("\"" + t + "\""
+ " is working.");
}
driver.navigate().back();
}
driver.quit();
}
}
http://www.qaautomated.com/2016/10/selenium-test-to-check-links-in-web.html