用于检查Selenium Webdriver中URL的辅助命令

问题描述 投票:6回答:3

由于该网页上的网址没有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");
xpath selenium-webdriver junit4
3个回答
18
投票

您可以使用'当前网址'对它进行验证,以

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

1
投票

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

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

0
投票

在Python中,它不是getCurrentUrl,而是

driver.current_url
© www.soinside.com 2019 - 2024. All rights reserved.