0

driver.getTitle() 不返回页面的标题。它返回空值。我在网页中有以下 html 结构:

<head>
  <!-- needed for translation of titles -->

  <!-- needed for gomez.jsp -->
  <script type="text/javascript" async="" src="https://ssl.google-analytics.com/ga.js"> </script><script type="text/javascript"> … </script><script src="https://B001E0.t.axf8.net/js/gtag5.js" type="text/javascript"></script>
  <title>
     Applications
  </title>
  so on....

有人可以解释一下这里的问题..提前谢谢

4

2 回答 2

2

然后你可能想要像下面这样显式等待:

    无效waitForLoad(WebDriver驱动程序){
        ExpectedCondition pageLoads = new
            预期条件(){
                公共布尔应用(WebDriver 驱动程序){
                    return (Boolean)((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                }
            };
        WebDriverWait 等待 = 新的 WebDriverWait(驱动程序, 30);
        等待.直到(页面加载);
    }

然后您可以执行以下操作:

    driver.getTitle()

于 2013-09-05T08:41:19.133 回答
1

我认为这对你来说可能已经晚了,但我只是把它放出来,以便其他人可以得到帮助。第一种蹩脚的方式是你可以添加

Thread.sleep(2000);
//(2000 is the time in milliseconds) before the the 
driver.getTitle();

你可以给时间任何你想要的,它会做的只是将你的执行时间减慢 2 秒(在这种情况下)

第二你可以使用

WebDriverWait wait = new WebD`enter code here`riverWait(driver, 10);
wait.until(ExpectedConditions.jsReturnsValue("complete"));
driver.getTitle();

只有当您的 HTML 和 JS 返回完整的文本时,它才会起作用,现在您将获得新的页面标题,您可以使用它进行验证。

向线程添加更多代码:

WebDriverWait wait = new WebDriverWait(driver, 30);
        try{
            wait.until(ExpectedConditions.titleIs(eTitle));
            Reporter.log("PASS: Title is matching.", true);
        }
于 2017-04-06T11:13:04.427 回答