Selenium:在启动网站后,我们如何检查是否启动了正确的页面

Selenium:在启动网站后,我们如何检查是否启动了正确的页面,selenium,webpage,launch,Selenium,Webpage,Launch,在Selenium中:启动网站后,我们如何检查是否启动了正确的页面 例如: 如果我想启动www.google.com,在运行代码后,我如何检查是否启动了相同的页面 我曾经 Assert.assertEquals("Correct web page",driver.findElement(By.Xpath("<xpath of one of the element in the page>")).isDisplayed ()); Assert.assertEquals(“正确的网页”

在Selenium中:启动网站后,我们如何检查是否启动了正确的页面

例如: 如果我想启动
www.google.com
,在运行代码后,我如何检查是否启动了相同的页面

我曾经

Assert.assertEquals("Correct web page",driver.findElement(By.Xpath("<xpath of one of the element in the page>")).isDisplayed ());
Assert.assertEquals(“正确的网页”,driver.findelelement(By.Xpath(“”).isDisplayed());
运行程序后,我得到以下错误:

Exception in thread "main" java.lang.AssertionError: expected:<Correct web page> but was:<true>
线程“main”java.lang.AssertionError中出现异常:应为:但为:
有许多方法可以断言加载的页面是否正确,最常用的是断言加载的url是否正确以及页面标题是否正确

为加载的正确URL断言:

String expectedUrl = "https://www.google.com";
WebDriver driver = new FirefoxDriver();
driver.get(expectedUrl);
try{
  Assert.assertEquals(expectedUrl, driver.getCurrentUrl());
  System.out.println("Navigated to correct webpage");
}
catch(Throwable pageNavigationError){
  System.out.println("Didn't navigate to correct webpage");
}
String expectedTitle = "Google";
String expectedUrl = "https://www.google.com";
WebDriver driver = new FirefoxDriver();
driver.get(expectedUrl);
try{
  Assert.assertEquals(expectedTitle, driver.getTitle());
  System.out.println("Navigated to correct webpage");
}
catch(Throwable pageNavigationError){
  System.out.println("Didn't navigate to correct webpage");
}
为页面标题断言:

String expectedUrl = "https://www.google.com";
WebDriver driver = new FirefoxDriver();
driver.get(expectedUrl);
try{
  Assert.assertEquals(expectedUrl, driver.getCurrentUrl());
  System.out.println("Navigated to correct webpage");
}
catch(Throwable pageNavigationError){
  System.out.println("Didn't navigate to correct webpage");
}
String expectedTitle = "Google";
String expectedUrl = "https://www.google.com";
WebDriver driver = new FirefoxDriver();
driver.get(expectedUrl);
try{
  Assert.assertEquals(expectedTitle, driver.getTitle());
  System.out.println("Navigated to correct webpage");
}
catch(Throwable pageNavigationError){
  System.out.println("Didn't navigate to correct webpage");
}

提供URL作为参数,并在页面加载后断言
驱动程序。getCUrrentUrl()
等于最初提供的URL。为了确认实际值是否与预期值相同,除非使用println语句,否则无法在控制台中看到任何内容。。这是正确的吗?我们可以为这两种情况提供打印声明。请看我编辑的回复。只需点击投票图标下方的右下角符号即可@user3702778:如果您需要更多关于如何接受回答的信息,请访问:“断言加载的URL是否正确”部分就是我要查找的内容,它可以正常工作。谢谢