有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

有关网页上的示例程序

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

我执行以下步骤:

  1. 用驱动程序运行程序。close()行代码被注释掉
  2. 该程序打开一个Firefox浏览器,然后在谷歌上搜索“qa自动化”一词
  3. 一旦“测试通过”消息已打印到屏幕(在控制台中),请转到谷歌搜索结果页面
  4. 使用浏览器菜单,进入工具/Web开发者/Page Source
  5. 在页面源页面上,搜索术语“qaautomation.net”
  6. 退出Firefox应用程序
  7. 手动打开Firefox和浏览器窗口,即不使用该程序
  8. 去谷歌。并手动搜索“qa自动化”
  9. 加载结果页面后,执行上述步骤4和5

我在第5步没有得到搜索结果,但在第9步得到了。这是为什么?两个页面源似乎来自同一个网页。在此方面的任何帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    与其在HTML源代码中搜索,我建议您只需刮取页面即可。这种方法可以让你更明确地定位你看到文本的地方。。。在页面上的链接href vs text 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");