使用selenium Firefox webdriver将 文本清空

我在Windows 7上使用Firefox 21和C#WebDriver 2.33。我很困惑为什么以下测试失败(我只是检查我的配置设置是否正确)。 这在其他浏览器中传递。

[Test] public void FirefoxDriverWorks() { var firefoxDriver = new FirefoxDriver(); TestGoogleStillExists(firefoxDriver); firefoxDriver.Quit(); } public void TestGoogleStillExists(IWebDriver webDriver) { webDriver.Navigate().GoToUrl("http://www.google.com"); var title = webDriver.FindElement(By.CssSelector("head title")); Assert.That(title.Text, Is.EqualTo("Google")); } 

Selenium WebDriver的textfunction只返回页面本身对用户可见的文本。 标题文本在技术上在页面上不可见(它显示在浏览器中chrome的标题部分)。

相反,Selenium WebDriver有一个方法可以返回您可以使用的页面标题 :

 driver.Title; 

所以你的代码变成:

 public void TestGoogleStillExists(IWebDriver webDriver) { webDriver.Navigate().GoToUrl("http://www.google.com"); var title = webDriver.Title; Assert.That(title, Is.EqualTo("Google")); }