@H_301_1@哪个是打开Url的首选方法(和之间有什么区别):
driver.Url = "http://example.com";
要么
driver.Navigate().GoToUrl("http://example.com");
另外,如果驱动程序已经指向同一页面,那么会再次设置网址导致页面刷新?
即
... driver.Url = "http://example.com"; driver.Url = "http://example.com"; //does this reload the page? ...
FWIW我正在使用Chrome驱动程序chromedriver.exe,但它似乎不是一个托管程序集(我尝试用ILSpy打开它,但没有运气).
解决方法
Selenium是一个开源框架,所以请看源码
here.
GoToUrl()在RemoteNavigator.cs中定义:
/// <summary> /// Navigate to a url for your test /// </summary> /// <param name="url">String of where you want the browser to go to</param> public void GoToUrl(string url) { this.driver.Url = url; } /// <summary> /// Navigate to a url for your test /// </summary> /// <param name="url">Uri object of where you want the browser to go to</param> public void GoToUrl(Uri url) { if (url == null) { throw new ArgumentNullException("url","URL cannot be null."); } this.driver.Url = url.ToString(); }
所以基本上是driver.Navigate().GoToUrl();设置driver.Url在引擎盖下,我没有看到差异.
然而,Driver.Navigate().GoToUrl()更灵活,它允许发送字符串或Uri作为参数类型,而只有字符串才允许通过driver.Url进行设置.
对于你的第二个问题,源代码显示driver.Navigate().Refresh()要求浏览器刷新,而driver.Url告诉浏览器导航.所以这两个根本不同.详情请参阅Difference between Refresh and Navigate function in browser control?
如果要刷新页面,请使用driver.Navigate().Refresh();
Refresh()在RemoteNavigator.cs中定义:
/// <summary> /// Refresh the browser /// </summary> public void Refresh() { // driver.SwitchTo().DefaultContent(); this.driver.InternalExecute(DriverCommand.Refresh,null); }
driver.Url定义在RemoteWebDriver.cs:
public string Url { ... set { ... try { this.Execute(DriverCommand.Get,parameters); } ... } }