我正在学习WebDriver,并试图检查demoaut网站上的链接。循环中的代码应该通过其标题识别“Under Construction”页面,打印出第一行,然后返回到基础URL。但是这不会因为某些原因而发生。第一个“正在建设中”的链接(特色度假目的地)没有被识别,提示错误的行被打印,然后由于NoSuchElementException而返回崩溃,因为它正在寻找错误的链接页。这是为什么发生?为什么它根据“正在建设中”页面的标题行事?WebDriver打印错误的条件语句
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;
//extract the link texts of each link element
for (WebElement e : linkElements) {
linkTexts[i] = e.getText();
i++;
}
//test each link
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();
}
}