webdriver.get() 和 webdriver.navigate() 之间的区别
get()
和 navigate()
方法有什么区别?
此方法或其他方法是否等待页面内容加载?
我真正需要的是像 Selenium 1.0 的 WaitForPageToLoad
这样的东西,但要通过 webdriver
使用。
有什么建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
driver.get()
:它用于访问特定网站,但它不维护浏览器历史记录和cookie,因此,我们无法使用前进和后退按钮,如果我们点击该按钮,页面将不会获取时间表driver.navigate()
:它用于转到特定网站,但它会维护浏览器历史记录和cookie ,所以我们可以使用前向和后退按钮用于在测试用例编码期间在页面之间导航driver.get()
: It's used to go to the particular website , But it doesn't maintain the browser History and cookies so , we can't use forward and backward button , if we click on that , page will not get scheduledriver.navigate()
: it's used to go to the particular website , but it maintains the browser history and cookies, so we can use forward and backward button to navigate between the pages during the coding of Testcase不确定它也适用于这里,但在量角器的情况下,当使用
navigate().to(...)
时,历史记录会被保留,但当使用get()
时它丢失了。我的一个测试失败了,因为我连续使用
get()
两次,然后执行navigate().back()
。因为历史记录丢失了,返回时跳转到about页面,抛出错误:Not sure it applies here also but in the case of protractor when using
navigate().to(...)
the history is being kept but when usingget()
it is lost.One of my test was failing because I was using
get()
2 times in a row and then doing anavigate().back()
. Because the history was lost, when going back it went to the about page and an error was thrown:根据 get() 的 javadoc,它是 Navigate.to() 的同义词
查看下面的 javadoc 屏幕截图:
get() 的 Javadoc 说明了一切 -
As per the javadoc for get(), it is the synonym for Navigate.to()
View javadoc screenshot below:
Javadoc for get() says it all -
driver.get()
用于导航特定 URL(网站)并等待页面加载。driver.navigate()
用于导航到特定 URL,并且不等待页面加载。它维护浏览器历史记录或 cookie 以向后或向前导航。driver.get()
is used to navigate particular URL(website) and wait till page load.driver.navigate()
is used to navigate to particular URL and does not wait to page load. It maintains browser history or cookies to navigate back or forward.当您第一次使用时,
navigate().to()
和get()
的工作方式相同。当您多次使用它时,然后使用navigate().to()
您可以随时转到上一页,而使用get()
也可以执行相同的操作。结论:navigate().to() 保存当前窗口的整个历史记录,而 get() 只是重新加载页面并保存所有历史记录。
navigate().to()
andget()
will work same when you use for the first time. When you use it more than once then usingnavigate().to()
you can come to the previous page at any time whereas you can do the same usingget()
.Conclusion:
navigate().to()
holds the entire history of the current window andget()
just reload the page and hold any history.就其价值而言,从我的 IE9 测试来看,包含 hashbang(在我的例子中是单页应用程序)的 URL 似乎存在差异:
driver.get("http://www.example. com#anotherpage")
方法由浏览器作为片段标识符进行处理,并且 JavaScript 变量将保留来自先前的 URL。而
navigate().to("http://www.example.com#anotherpage")
方法由浏览器作为地址/位置/URL 栏输入和 JavaScript 处理不会保留先前 URL 中的变量。For what it's worth, from my IE9 testing, it looks like there's a difference for URLs that contain a hashbang (a single page app, in my case):
The
driver.get("http://www.example.com#anotherpage")
method is handled by the browser as a fragment identifier and JavaScript variables are retained from the previous URL.While, the
navigate().to("http://www.example.com#anotherpage")
method is handled by the browser as a address/location/URL bar input and JavaScript variables are not retained from the previous URL.webdriver.get()
和webdriver.navigate()
方法之间存在一些差异。get()
根据 API 文档 get() 方法/selenium/WebDriver.html" rel="nofollow noreferrer">WebDriver 接口扩展了 SearchContext 定义为:
用法:
navigate()
另一方面, navigate() 是 抽象 允许 WebDriver 实例,即用于访问浏览器历史记录以及导航到给定 URL 的
驱动程序
。方法及用法如下:to(java.lang.String url)
:在当前浏览器窗口加载一个新的网页。to(java.net.URL url)
:to(String) 的重载版本,可以轻松传递 URL。refresh()
:刷新当前页面。back()
:向后移动浏览器历史记录中的单个“项目”。forward()
:在浏览器历史记录中向前移动单个“项目”。There are some differences between
webdriver.get()
andwebdriver.navigate()
method.get()
As per the API Docs get() method in the WebDriver interface extends the SearchContext and is defined as:
Usage:
navigate()
On the other hand, navigate() is the abstraction which allows the WebDriver instance i.e. the
driver
to access the browser's history as well as to navigate to a given URL. The methods along with the usage are as follows:to(java.lang.String url)
: Load a new web page in the current browser window.to(java.net.URL url)
: Overloaded version of to(String) that makes it easy to pass in a URL.refresh()
: Refresh the current page.back()
: Move back a single "item" in the browser's history.forward()
: Move a single "item" forward in the browser's history.driver.get("url")
和driver.navigate( ).to("url")
都是相同/同义的。to("url")
内部调用get("url")
方法。请查找以下图片以供参考。它们都不存储历史记录 - 这是大多数博客/网站上提供的错误信息。
下面的语句 1、2 和 3、4 将执行相同的操作,即到达给定的 URL。
只有
navigate()
可以执行不同的操作,即后退、前进等。但to("url")
方法不行。driver.get("url")
anddriver.navigate( ).to("url")
both are same/synonymous.to("url")
internally callingget("url")
method. Please find the below image for reference.Either of them does not store history - this is the wrong information that is available on most of the blogs/websites.
Below, statements 1, 2, and 3, 4 will do the same things i.e land in the given URL.
Only
navigate()
can do different things i.e. moving back, forward, etc. But not theto("url")
method.否则,您可能需要 get 方法:
据我所知,Navigate 允许您使用浏览器历史记录。
Otherwise you prob want the get method:
Navigate allows you to work with browser history as far as i understand it.
两者执行相同的功能,但 driver.get();似乎更受欢迎。
当您已经处于脚本中间并且想要从当前 URL 重定向到新 URL 时,最好使用
driver.navigate().to();
。为了区分您的代码,您可以使用driver.get();
在打开浏览器实例后启动第一个 URL,尽管两种方式都可以工作。Both perform the same function but driver.get(); seems more popular.
driver.navigate().to();
is best used when you are already in the middle of a script and you want to redirect from current URL to a new one. For the sake of differentiating your codes, you can usedriver.get();
to launch the first URL after opening a browser instance, albeit both will work either way.CASE-1
在下面的代码中,我导航到 3 个不同的 URL,当执行导航命令时,它导航回 facebook 主页。
CASE-2:
在下面的代码中,我使用了 navigation() 而不是 get(),但是两个片段(Case-1 和 Case-2)的工作原理完全相同,只是 case-2 的执行时间少于case-1
执行相同的任务,但使用 navigator() 你可以移动
back() 或forward() 在您的会话历史记录中。
页面完全加载。
CASE-1
In the below code I navigated to 3 different URLs and when the execution comes to navigate command, it navigated back to facebook home page.
CASE-2:
In below code, I have used navigate() instead of get(), but both the snippets(Case-1 and Case-2) are working exactly the same, just the case-2 execution time is less than of case-1
performing the same task but with the use of navigate() you can move
back() or forward() in your session's history.
the page to load fully or completely.
driver.get(url)
和navigate.to(url)
均用于转到特定网页。关键的区别在于driver.get(url)
:它不维护浏览器历史记录和cookie,并等待页面完全加载。driver.navigate.to(url)
:它也用于转到特定网页。它维护浏览器历史记录和 cookie,并且不会等到页面完全加载并在页面之间向后、向前导航并刷新。driver.get(url)
andnavigate.to(url)
both are used to go to particular web page. The key difference is thatdriver.get(url)
: It does not maintain the browser history and cookies and wait till page fully loaded.driver.navigate.to(url)
:It is also used to go to particular web page.it maintain browser history and cookies and does not wait till page fully loaded and have navigation between the pages back, forward and refresh.为了更好地理解它,我们必须了解 Selenium WebDriver 的架构。
只需访问 https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
并搜索“导航到新 URL”。文本。您将看到 GET 和 POST 两种方法。
因此得出以下结论:
driver.get()
方法在内部向 Selenium Server Standalone 发送 Get 请求。而driver.navigate()
方法将 Post 请求发送到 Selenium Server Standalone。To get a better understanding on it, one must see the architecture of Selenium WebDriver.
Just visit https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
and search for "Navigate to a new URL." text. You will see both methods GET and POST.
Hence the conclusion given below:
driver.get()
method internally sends Get request to Selenium Server Standalone. Whereasdriver.navigate()
method sends Post request to Selenium Server Standalone.(已添加强调)
(Emphasis added)
他们似乎都导航到给定的网页并引用 @matt 答案:
单页应用程序是一个例外。
这两种方法之间的区别不在于它们的行为,而在于应用程序的工作方式以及浏览器处理它的方式的行为。
navigate().to()
通过更改 URL 来导航到页面,就像进行向前/向后导航一样。而
get()
会刷新页面以更改 URL。因此,在应用程序域发生变化的情况下,这两种方法的行为类似。也就是说,这两种情况下都会刷新页面。但是,在单页应用程序中,虽然
navigate().to()
不会刷新页面,但get()
会刷新页面。此外,这就是由于刷新应用程序而使用
get()
时浏览器历史记录丢失的原因。最初回答:https://stackoverflow.com/a/33868976/3619412
They both seems to navigate to the given webpage and quoting @matt answer:
Single-Page Applications are an exception to this.
The difference between these two methods comes not from their behavior, but from the behavior in the way the application works and how browser deal with it.
navigate().to()
navigates to the page by changing the URL like doing forward/backward navigation.Whereas,
get()
refreshes the page to changing the URL.So, in cases where application domain changes, both the method behaves similarly. That is, page is refreshed in both the cases. But, in single-page applications, while
navigate().to()
do not refreshes the page,get()
do.Moreover, this is the reason browser history is getting lost when
get()
is used due to application being refreshed.Originally answered: https://stackoverflow.com/a/33868976/3619412