硒:启动网站后,我们如何检查是否启动了正确的页面 [英] Selenium :After launching a website how could we check if the right page is launched

查看:93
本文介绍了硒:启动网站后,我们如何检查是否启动了正确的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Selenium :After launching a website how could we check if the right page is launched or not.

示例: 如果要启动www.google.com,请在运行代码后如何检查是否已启动同一页面.

Example: If I want to launch www.google.com,after running the code how do I check if the same page has been launched.

我用过

Assert.assertEquals("Correct web page",driver.findElement(By.Xpath("<xpath of one of the element in the page>")).isDisplayed ());

运行程序后,出现以下错误:

After running the program, I got the below error:

Exception in thread "main" java.lang.AssertionError: expected:<Correct web page> but was:<true>

推荐答案

有许多方法可以断言正确的页面加载,最常用的是断言正确的URL和页面标题.

There are many ways to assert for correct page loaded, most used are the assert for correct url loaded and page title.

为正确的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");
}

这篇关于硒:启动网站后,我们如何检查是否启动了正确的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆