我的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>
-
验证页面头文本,我使用下面的代码:
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());
-
错误:
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("//*[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);
使用findElements()
代替,并在验证之前将结果连接到一个字符串中。您可能想要规范化空格(即修剪前导/尾部空白,将换行符转换为空白,将多个空白转换为单个空格),并进行不区分大小写的比较。
您应该能够从<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());