added some waits. got rid of check response it was not working in my computer.
[code:2u9pnv8n]
import org.junit.runner.Request;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Specificlinks_Second {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
driver = new FirefoxDriver();
driver.get("http://www.bbc.com/sport/0/");
// here we have found five links which have the similar pattern so we
// divided the xpath on thebasis of that pattern
String part1 = "//*[@id='programme-links-hyper']/ul/li[";
String part2 = "]/a";
// for(int i=1;i<=6;i++)
// Now here we will declare the while loop from which we will call teh
// iselemntpresent function
int i = 1;
while (iEelementPresent(part1 + i + part2)) {
String text = driver.findElement(By.xpath(part1 + i + part2)).getText();
System.out.println(text);
String url = driver.findElement(By.xpath(part1 + i + part2)).getAttribute("href");
System.out.println(url);
if (checkresponse(url)) {
driver.findElement(By.xpath(part1 + i + part2)).click();
System.out.println(driver.getTitle());
// driver.get("http://www.bbc.com/sport/0/");
Thread.sleep(2000);
driver.navigate().back();
Thread.sleep(2000);
} else {
System.out.println("Something is wrong");
}
Thread.sleep(2000);
i++;
}
}
// we will make a function which will return true if the element with the
// given xpath is present or false if the element is not present
public static boolean iEelementPresent(String elementxpath) {
int count = driver.findElements(By.xpath(elementxpath)).size();
if (count == 0) {
return false;
} else {
return true;
}
}
public static boolean checkresponse(String url) {
try {
// int resp_code =
// Request.Get(url).execute().returnResponse().getStatusLine().getStatusCode();
// if (resp_code == 200) {
// return true;
// } else {
// return false;
// }
return true;
} catch (Exception e) {
return false;
}
}
}[/code:2u9pnv8n]