`driver.get(url)` 与 `driver.navigate().to(url);` 的区别:

huangapple go评论79阅读模式
英文:

driver.get(url) vs driver.navigate().to(url);

问题

Google的回答:
get()用于访问特定的URL(网站),并等待页面加载完成。driver.navigate()用于导航到特定的URL,但不会等待页面加载完成。

Selenium文档:
文档的document.readyState属性描述了当前文档的加载状态。默认情况下,WebDriver在响应driver.get()或driver.navigate().to()调用时会等待文档的就绪状态完成。

我的问题是,在Google上说navigate方法不会等到页面加载完成,这与从Selenium文档中添加的观点不一致。请帮我理解。

英文:

Google's Answer:
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.

Selenium Documentation:
The document.readyState property of a document describes the loading state of the current document. By default, WebDriver will hold off on responding to a driver.get() (or) driver.navigate().to() call until the document ready state is complete

My query is in Google it was said, navigate method doesnot wait till the page loads which was not in line with the point added from Selenium Documentation.
Please help me to understand.

答案1

得分: 1

运行脚本时,我们首先要做的是打开浏览器并加载网页。我们通常使用 driver.get("url"); 来加载网页。每次使用此命令时,页面都将被刷新。

我们还可以使用 driver.navigate().to("url"); 来加载网页。这两个命令在行为方面的工作方式相同。但是,navigate().to() 还具有其他功能,如 navigate().forward()navigate().back()navigate().refresh()

因此,区别在于 driver.get() 不会存储历史记录,而 driver.navigate().to() 会存储浏览器历史记录,以便用于其他向前和向后等命令。

在单页面应用程序中,使用 navigate().to() 导航到页面时会更改 URL,就像进行前进/后退一样,而 get() 则会刷新页面。

更多信息请参见此处 - https://stackoverflow.com/questions/5664808/difference-between-webdriver-get-and-webdriver-navigate

英文:

The first thing we do when run the script is to open the browser and load the web page. We use commonly driver.get(“url”); to load the webpage. Every time we use this command, the page will be refreshed.

We can also use driver.navigate().to(“url’); to load the webpage. Both the commands work in the same way in terms of behavior. But the navigate().to() also have the other functions such as navigate().forward(), navigate().back() and navigate().refresh().

So the difference is driver.get() never stores history whereas driver.navigate().to() stores browser history so as to be used for other commands forward and back etc.

In single page applications while navigate().to() navigates to the page by changing URL like doing forward/backward, get() refreshes page.

More info here - https://stackoverflow.com/questions/5664808/difference-between-webdriver-get-and-webdriver-navigate

答案2

得分: 0

在简单的术语中,get() 方法在 WebDriver 接口中扩展了 SearchContext,并被定义为:

/**
 * 在当前浏览器窗口中加载一个新的网页。这是通过 HTTP POST 操作完成的,
 * 该方法将阻塞,直到加载完成。
 * 这将遵循服务器发出的重定向,要么是从返回的 HTML 中作为元重定向发出的。
 * {@link org.openqa.selenium.WebDriver.Navigation#to(String)} 的同义词。
 */
void get(String url);

因此,您可以使用:

driver.get("https://www.google.com/");

另一方面,navigate() 是允许 WebDriver 实例(即 driver)访问浏览器历史并导航到给定 URL 的 抽象。方法及其用法如下:

  • to(java.lang.String url):在当前浏览器窗口中加载一个新的网页。
driver.navigate().to("https://www.google.com/");
  • to(java.net.URL url):to(String) 的重载版本,方便传递 URL。
  • refresh():刷新当前页面。
driver.navigate().refresh();
  • back():在浏览器历史中后退一个“项目”。
driver.navigate().back();
  • forward():在浏览器历史中前进一个“项目”。
driver.navigate().forward();
英文:

In simple words get() method in the WebDriver interface extends the SearchContext and is defined as:

/**
 * Load a new web page in the current browser window. This is done using an HTTP POST operation,
 * and the method will block until the load is complete.
 * This will follow redirects issued either by the server or as a meta-redirect from within the
 * returned HTML.
 * Synonym for {@link org.openqa.selenium.WebDriver.Navigation#to(String)}.
 */
void get(String url);

Hence you can use:

driver.get("https://www.google.com/");

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.

    driver.navigate().to("https://www.google.com/");
    
  • to(java.net.URL url): Overloaded version of to(String) that makes it easy to pass in a URL.

  • refresh(): Refresh the current page.

    driver.navigate().refresh();
    
  • back(): Move back a single "item" in the browser's history.

    driver.navigate().back();
    
  • forward(): Move a single "item" forward in the browser's history.

    driver.navigate().forward();
    

答案3

得分: 0

//方便方式
driver.get("https://selenium.dev");

//较长方式
driver.navigate().to("https://selenium.dev");

28/08/2022

这两者之间没有区别,只是一个是长形式,另一个是 Java 的短形式。

https://www.selenium.dev/documentation/webdriver/browser/navigation/
英文:
//Convenient
driver.get("https://selenium.dev");

//Longer way
driver.navigate().to("https://selenium.dev");

28/08/2022

There is no difference between the two, just that one is the long form and the other is the short form of Java.

https://www.selenium.dev/documentation/webdriver/browser/navigation/

huangapple
  • 本文由 发表于 2020年7月26日 23:24:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63102108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定