如何在selenium webdriver中验证标题

时间:2017-01-25 10:39:21

标签: java selenium

我试图在主页中验证标题,我尝试过以下两种方法,但是我收到了错误。

1

WebDriverWait wait=new WebDriverWait(driver,30);       
wait.until(ExpectedConditions.titleContains("This is demo site for"));

ERROR:

Timed out after 30 seconds waiting for title to contain "This is demo site for". Current title: "Home page"
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'

2

String actualTitle=driver.getTitle();
String expectedTitle="This is demo site for";
assertEquals(actualTitle, expectedTitle);

ERROR:

org.junit.ComparisonFailure: expected:<[Home page]> but was:<[This is demo site for]>

HTML:

<div class="page-title">
  <h2>
    This is demo site for
    <img alt="" src="image link here"></img>
  </h2>
</div>

以上是该页面标题的DOM。我不知道如何使用这个元素,请任何人都可以帮助我。

2 个答案:

答案 0 :(得分:2)

您似乎混淆<title>This is demo site for</title>中应该存在的实际网页标题(<head>)只有位于页面<body>中某处的简单标题字符串

请尝试以下操作:

String actualTitle = driver.findElement(By.xpath('//div[@class="page-title"]/h2'))getText();
String expectedTitle = "This is demo site for";
assertEquals(actualTitle, expectedTitle);

答案 1 :(得分:1)

我在元素中验证标题的解决方案:

mutate_all

或其他方式:

map