【发布时间】:2017-03-31 09:09:54
【问题描述】:
我正在学习 WebDriver,只是想查看 demoaut 网站上的链接。循环中的代码应该通过标题识别“建设中”页面,打印出第一行,然后返回基本 url。但由于某种原因,这不会发生。它到达的第一个“正在建设”链接(特色度假目的地)未被识别,提示打印错误的行,然后由于 NoSuchElementException 而不是返回它崩溃,因为它正在寻找错误的链接页。为什么会这样?为什么不根据“建设中”页面的标题进行操作?
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();
}
}
【问题讨论】:
标签: java eclipse selenium testing selenium-webdriver