用于检查selenium webdriver中的url的断言命令

时间:2015-01-05 08:01:57

标签: xpath selenium-webdriver junit4

由于网页上的网址没有xpath和id,我如何检查我的实际网址是否与预期的网址匹配?

我在下面提供了我的代码,但它没有用。

String Actualtext = driver.findElement(By.linkText("http://localhost:8080/imdb/homepage")).getText();
Assert.assertEquals(Actualtext, "http://localhost:8080/imdb/homepage" );
System.out.println("URL matching --> Part executed");

4 个答案:

答案 0 :(得分:17)

您可以根据“当前网址”对其进行验证。如

String URL = driver.getCurrentUrl();
Assert.assertEquals(URL, "http://localhost:8080/imdb/homepage" );

答案 1 :(得分:1)

getText()用于获取可见的innertText,您可以使用getAttribute方法获取网址(用于超链接),如下所示

String Actualtext = driver.findElement("YourElementLocator").getAttribute("href")
Assert.assertEquals(Actualtext, "http://localhost:8080/imdb/homepage" );
System.out.println("URL matching --> Part executed");

答案 2 :(得分:0)

在Python中,它不是getCurrentUrl,而是

driver.current_url

答案 3 :(得分:-1)

在Selenium Webdriver C#中

字符串URL = driver.Url; Assert.AreEqual(URL,“ http://localhost:8080/imdb/homepage”);

在Selenium Webdriver Java中

字符串URL = driver.getCurrentUrl(); Assert.assertEquals(URL,“ http://localhost:8080/imdb/homepage”);