I am learning WebDriver and just trying to check the links on the demo website. The code in the loop should recognize the “Under Construction” page by its name, print the first line, and then return to the base URL. But for some reason this does not happen. The very first “building” link that it receives (recognized places to stay) is not recognized as such, it requests the wrong line for printing, and then instead of returning, it fails due to a NoSuchElementException exception, because it is looking for a link to wrong page. Why is this happening? Why doesn’t it act on the page title "Under Construction"?
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CheckLinks {
public static void main(String[] args) {
String baseUrl = "http://newtours.demoaut.com/";
System.setProperty("webdriver.gecko.driver", "C:\\Workspace_e\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String underConsTitle = "Under Construction: Mercury Tours";
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;
for (WebElement e : linkElements) {
linkTexts[i] = e.getText();
i++;
}
for (String t : linkTexts) {
driver.findElement(By.linkText(t)).click();
if (driver.getTitle().equals(underConsTitle)) {
System.out.println("\"" + t + "\""
+ " is under construction.");
} else {
System.out.println("\"" + t + "\""
+ " is working.");
}
driver.navigate().back();
}
driver.quit();
}
}