2016-11-10 53 views
-3

我的HTML代码如下:如何使用单个xpath验证span标记文本?

<div class="row"> 
<div class="col-lg-7"> 
<h1 class="h1_home"> 
<span>Welcome to the</span> 
<br/> 
<span>Automation Software Testing</span> 
</h1> 
<p class="col-xs-9 col-lg-12"> 
</div> 
</div> 
  1. 验证页面页眉文字我用波纹管代码:

    String expected_txt = "Welcome to the Automation Software Testing"; 
    WebElement header_txt_elm=driver.findElement(By.xpath(".//h1[@class='h1_home']//span")); 
    String actual_headertxt = header_txt_elm.getText().toString(); 
    Assert.assertEquals(actual_headertxt.toLowerCase(), expected_txt.toLowerCase()); 
    
  2. 收到错误:

    java.lang.AssertionError: expected [Welcome to the Automation Software Testing] but found [welcome to the] 
    
+1

仅供参考......你并不需要在'actual_headertxt'的'的ToString()'。 '.getText()'已经返回一个字符串。 – JeffC

回答

0

使用findElements(),并在验证之前将结果连接成一个字符串。您可能需要对空格进行规范化(即修剪前导空格/尾部空格,将换行符转换为空格,将多个空格转换为单个空格),并进行不区分大小写的比较。

0

您应该能够从<h1>中提取所有文本。

String expected_txt = "Welcome to the Automation Software Testing"; 
WebElement header_txt_elm = driver.findElement(By.xpath("//h1[@class='h1_home']")); 
// WebElement header_txt_elm = driver.findElement(By.cssSelector("h1.h1_home")); // CSS selector version 

String actual_headertxt = header_txt_elm.getText(); 
Assert.assertEquals(actual_headertxt.toLowerCase(), expected_txt.toLowerCase()); 
1
String expected_txt = "Welcome to the Automation Software Testing"; 
WebElement header_txt_elm = driver.findElement(By.xpath("//*[text()='Welcome to the']")); 
WebElement header_txt_elm2 = driver.findElement(By.xpath("//*[text()='Automation Software Testing']")); 
String actual_txt1=header_txt_elm.getText(); 
String actual_txt2=header_txt_elm2.getText(); 
String actual_txt=actual_txt1+actual_txt2; 
Assert.assertEquals(actual_headertxt, expected_txt); 
+0

示例不够!这将是很好,如果有效的解释给出!谢谢!! –

+0

我从Web元素中获取2个文本值。比我保存的字符串值,我串联成一个字符串的字符串。那么我将这些值与实际的文本进行比较。 –

1
String expected_value = "Welcome to the Automation Software Testing"; 
WebElement header_value_elm = driver.findElement(By.xpath("//*[text()='Welcome to the']")); 
WebElement header_value_elm2 = driver.findElement(By.xpath("//*[text()='Automation Software Testing']")); 
String actual_txt1=header_txt_elm.getText(); 
String actual_txt2=header_txt_elm2.getText(); 
String actual_txt=actual_txt1+actual_txt2;`enter code here` 
Assert.assertEquals(actual_headertxt, expected_txt); 
相关问题