如何使用硒 Webdriver 验证网页上的错误消息?
我们可以使用 Selenium 驱动程序和断言来验证网页上的错误消息。如果实际值和预期值不匹配,就会抛出断言错误。

我们尝试来验证高亮显示的错误消息。

示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.testng.Assert;
public class VerifyErrorMsg{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//URL launch
driver.get("https://www.linkedin.com/");
// identify element
WebElement l = driver.findElement(By.id("session_key"));
l.sendKeys("abc");
WebElement t = driver.findElement(By.className("sign-in-form__submit-button"));
t.click();
//expected error text
String exp = "Please enter a valid email address or mobile number.";
//identify actual error message
WebElement m = driver.findElement(By.className("alert-content"));
String act = m.getText();
System.out.println("Error message is: "+ act);
//verify error message with Assertion
Assert.assertEquals(exp, act);
driver.quit();
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP