Java 如何使用单个xpath验证span标记文本?

Java 如何使用单个xpath验证span标记文本?,java,selenium-webdriver,testng,Java,Selenium Webdriver,Testng,我的HTML代码如下: <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"> <

我的HTML代码如下:

<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>
  • 获取错误:

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

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

    您应该能够从
    中提取所有文本


    仅供参考。。。您不需要
    实际\u headerText
    上的
    .toString()
    .getText()
    已返回字符串。示例还不够!!如果能给出合理的解释那就太好了!!谢谢!!我从web元素中获取2个文本值。然后保存字符串值,并将字符串连接成单个字符串。然后我将这些值与实际文本进行比较。
    java.lang.AssertionError: expected [Welcome to the Automation Software Testing] but found [welcome to the]
    
    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());
    
    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);
    
    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);