对于网页上的示例程序
http://www.qaautomation.net/?p=263
我执行以下步骤:
我在步骤5中没有搜索结果,但我在步骤9中没有搜索结果。为什么会这样?两个页面源似乎都来自同一个网页。任何有关这方面的帮助将不胜感激。
答案 0 :(得分:0)
我建议您只是抓取页面,而不是搜索HTML源代码。这个方法可以让你更具体地定位你看到文本的位置...在链接href vs页面上的文字vs ???等。
下面的代码演示了两个不同的地方,你可以找到" qaautomation.net"文本。第一个是在标题链接href中,第二个是在标题href正下方的引文链接中。
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("qa automation\n"); // need the \n to simulate hitting ENTER to start the search
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.titleContains("Google Search")); // need to wait until the results page loads
List<WebElement> links = driver.findElements(By.cssSelector("h3.r > a"));
// System.out.println("links: " + links.size()); // for debugging
for (WebElement link : links)
{
// looking for qaautomation.net in the link href
if (link.getAttribute("href").contains("qaautomation.net"))
{
System.out.println("PASS: found in href");
}
}
List<WebElement> cites = driver.findElements(By.tagName("cite"));
// System.out.println("cites: " + cites.size()); // for debugging
for (WebElement cite : cites)
{
// looking for qaautomation.net in the citation (text below the heading link)
if (cite.getText().contains("qaautomation.net"))
{
System.out.println("PASS: found in cite");
}
}
System.out.println("DONE");