Selenium:找到基本网址

use*_*530 8 c# url selenium

我在不同的机器上使用Selenium来自动测试MVC Web应用程序.

我的问题是我无法获得每台机器的基本URL.

我可以使用以下代码获取当前url:

IWebDriver driver = new FirefoxDriver();
string currentUrl = driver.Url;
Run Code Online (Sandbox Code Playgroud)

但是,当我需要导航到不同的页面时,这无济于事.

理想情况下,我可以使用以下内容导航到不同的页面:

driver.Navigate().GoToUrl(baseUrl+ "/Feedback");
driver.Navigate().GoToUrl(baseUrl+ "/Home");
Run Code Online (Sandbox Code Playgroud)

我使用的可能解决方法是:

string baseUrl = currentUrl.Remove(22); //remove everything from the current url but the base url
driver.Navigate().GoToUrl(baseUrl+ "/Feedback");
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法可以做到这一点?

Arr*_*ran 17

解决此问题的最佳方法是创建UriURL 的实例.

这是因为.NET中的Uri 已经有代码完全为您完成此操作,所以您应该使用它.我会选择(未经测试的代码):

string url = driver.Url; // get the current URL (full)
Uri currentUri = new Uri(url); // create a Uri instance of it
string baseUrl = currentUri.Authority; // just get the "base" bit of the URL
driver.Navigate().GoToUrl(baseUrl + "/Feedback"); 
Run Code Online (Sandbox Code Playgroud)

从本质上讲,你是后管局物业的内部Uri类.

请注意,有一个属性做类似的事情,称为主机,但这不包括您的网站所做的端口号.但是要记住这一点.