为什么Firefox页面源中缺少“qaautomation.net”?

时间:2016-04-22 14:16:11

标签: java html selenium firefox

对于网页上的示例程序

http://www.qaautomation.net/?p=263

我执行以下步骤:

  1. 使用注释掉的driver.close()代码行运行程序。
  2. 该程序打开Firefox浏览器,然后搜索术语" qa automation"在谷歌上。
  3. 一旦测试通过。"消息已打印到屏幕上(在控制台中),转到谷歌搜索结果页面。
  4. 使用浏览器菜单,转到工具/ Web开发人员/页面源。
  5. 在页面源页面上,搜索术语" qaautomation.net"。
  6. 退出Firefox应用程序。
  7. 手动打开Firefox和浏览器窗口,即不使用该程序。
  8. 转到google.com并手动搜索" qa automation"。
  9. 结果页面加载后,执行上面的步骤4和步骤。
  10. 我在步骤5中没有搜索结果,但我在步骤9中没有搜索结果。为什么会这样?两个页面源似乎都来自同一个网页。任何有关这方面的帮助将不胜感激。

1 个答案:

答案 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");
相关问题