webdriver.get() 和 webdriver.navigate() 之间的区别

发布于 2024-11-01 07:24:09 字数 189 浏览 1 评论 0 原文

get()navigate() 方法有什么区别? 此方法或其他方法是否等待页面内容加载? 我真正需要的是像 Selenium 1.0 的 WaitForPageToLoad 这样的东西,但要通过 webdriver 使用。

有什么建议吗?

What is the difference between get() and navigate() methods?
Does any of this or maybe another method waits for page content to load?
What do I really need is something like Selenium 1.0's WaitForPageToLoad but for using via webdriver.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

仙女山的月亮 2024-11-08 07:24:10

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 schedule

driver.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

2024-11-08 07:24:10

不确定它也适用于这里,但在量角器的情况下,当使用 navigate().to(...) 时,历史记录会被保留,但当使用 get() 时它丢失了。

我的一个测试失败了,因为我连续使用 get() 两次,然后执行 navigate().back()。因为历史记录丢失了,返回时跳转到about页面,抛出错误:

Error: Error while waiting for Protractor to sync with the page: {}

Not sure it applies here also but in the case of protractor when using navigate().to(...) the history is being kept but when using get() it is lost.

One of my test was failing because I was using get() 2 times in a row and then doing a navigate().back(). Because the history was lost, when going back it went to the about page and an error was thrown:

Error: Error while waiting for Protractor to sync with the page: {}
眼泪都笑了 2024-11-08 07:24:10

根据 get() 的 javadoc,它是 Navigate.to() 的同义词

查看下面的 javadoc 屏幕截图:

javadoc Screenshot

get() 的 Javadoc 说明了一切 -

在当前浏览器窗口中加载新网页。这是使用完成的
HTTP GET 操作,该方法将阻塞,直到加载完成
完全的。这将遵循服务器发出的重定向或作为
从返回的 HTML 中进行元重定向。是否应该进行元重定向
“休息”任何持续时间,最好等到这个超时
已经结束了,因为当你的测试进行时底层页面是否会发生变化
对该接口的未来调用的执行结果将是
针对新加载的页面。 同义词
org.openqa.selenium.WebDriver。 Navigation.to(String).

As per the javadoc for get(), it is the synonym for Navigate.to()

View javadoc screenshot below:

javadoc screenshot

Javadoc for get() says it all -

Load a new web page in the current browser window. This is done using
an HTTP GET 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. Should a meta-redirect
"rest" for any duration of time, it is best to wait until this timeout
is over, since should the underlying page change whilst your test is
executing the results of future calls against this interface will be
against the freshly loaded page. Synonym for
org.openqa.selenium.WebDriver.Navigation.to(String).

昇り龍 2024-11-08 07:24:10

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.

狂之美人 2024-11-08 07:24:10

当您第一次使用时,navigate().to()get() 的工作方式相同。当您多次使用它时,然后使用 navigate().to() 您可以随时转到上一页,而使用 get() 也可以执行相同的操作。

结论:navigate().to() 保存当前窗口的整个历史记录,而 get() 只是重新加载页面并保存所有历史记录。

navigate().to() and get() will work same when you use for the first time. When you use it more than once then using navigate().to() you can come to the previous page at any time whereas you can do the same using get().

Conclusion: navigate().to() holds the entire history of the current window and get() just reload the page and hold any history.

黎夕旧梦 2024-11-08 07:24:10

就其价值而言,从我的 IE9 测试来看,包含 hashbang(在我的例子中是单页应用程序)的 URL 似乎存在差异:

http://www.example.com#page

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):

http://www.example.com#page

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.

伴梦长久 2024-11-08 07:24:10

webdriver.get()webdriver.navigate() 方法之间存在一些差异。


get()

根据 API 文档 get() 方法/selenium/WebDriver.html" rel="nofollow noreferrer">WebDriver 接口扩展了 SearchContext 定义为:

/**
 * 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);
    
  • 用法:

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

navigate()

另一方面, navigate()抽象 允许 WebDriver 实例,即用于访问浏览器历史记录以及导航到给定 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();
    

There are some differences between webdriver.get() and webdriver.navigate() method.


get()

As per the API Docs 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);
    
  • Usage:

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

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.

    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();
    
李不 2024-11-08 07:24:10
  1. driver.get("url")driver.navigate( ).to("url") 都是相同/同义的。

  2. to("url") 内部调用 get("url") 方法。请查找以下图片以供参考。

  3. 它们都不存储历史记录 - 这是大多数博客/网站上提供的错误信息。

  4. 下面的语句 1、2 和 3、4 将执行相同的操作,即到达给定的 URL。

    语句 1: driver.get("http://www.google.com");   
    语句2:driver.navigate().to(“http://www.amazon.in”);
    
    语句 3: driver.get("http://www.google.com");  
    语句 4: driver.get("http://www.amazon.in");
    
  5. 只有 navigate() 可以执行不同的操作,即后退、前进等。但 to("url") 方法不行。

胡佛get() 方法

  1. driver.get("url") and driver.navigate( ).to("url") both are same/synonymous.

  2. to("url") internally calling get("url") method. Please find the below image for reference.

  3. Either of them does not store history - this is the wrong information that is available on most of the blogs/websites.

  4. Below, statements 1, 2, and 3, 4 will do the same things i.e land in the given URL.

    statemnt 1: driver.get("http://www.google.com");   
    statemnt 2: driver.navigate( ).to("http://www.amazon.in");
    
    statemnt 3: driver.get("http://www.google.com");  
    statemnt 4: driver.get("http://www.amazon.in");
    
  5. Only navigate() can do different things i.e. moving back, forward, etc. But not the to("url") method.

hoover on get() method

撞了怀 2024-11-08 07:24:10

否则,您可能需要 get 方法:

Load a new web page in the current browser window. This is done using an
HTTP GET operation, and the method will block until the load is complete.

据我所知,Navigate 允许您使用浏览器历史记录。

Otherwise you prob want the get method:

Load a new web page in the current browser window. This is done using an
HTTP GET operation, and the method will block until the load is complete.

Navigate allows you to work with browser history as far as i understand it.

嘿咻 2024-11-08 07:24:10

两者执行相同的功能,但 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 use driver.get();to launch the first URL after opening a browser instance, albeit both will work either way.

深巷少女 2024-11-08 07:24:10

CASE-1

在下面的代码中,我导航到 3 个不同的 URL,当执行导航命令时,它导航回 facebook 主页。

public class FirefoxInvoke {
                @Test
                public static void browserInvoke()
                {
                    System.setProperty("webdriver.gecko.driver", "gecko-driver-path");
                WebDriver driver=new FirefoxDriver();
                System.out.println("Before"+driver.getTitle());
                driver.get("http://www.google.com");
                driver.get("http://www.facebook.com");
                driver.get("http://www.india.com");
                driver.navigate().back();
                driver.quit();
                }

                public static void main(String[] args) {
                    // TODO Auto-generated method stub
            browserInvoke();
                }

            }

CASE-2:

在下面的代码中,我使用了 navigation() 而不是 get(),但是两个片段(Case-1 和 Case-2)的工作原理完全相同,只是 case-2 的执行时间少于case-1

public class FirefoxInvoke {
                @Test
                public static void browserInvoke()
                {
                    System.setProperty("webdriver.gecko.driver", "gecko-driver-path");
                WebDriver driver=new FirefoxDriver();
                System.out.println("Before"+driver.getTitle());
                driver.navigate().to("http://www.google.com");
                driver.navigate().to("http://www.facebook.com");
                driver.navigate().to("http://www.india.com");
                driver.navigate().back();
                driver.quit();
                }

                public static void main(String[] args) {
                    // TODO Auto-generated method stub
            browserInvoke();
                }

            }
  • 所以 get() 和 navigate() 之间的主要区别是,两者都是
    执行相同的任务,但使用 navigator() 你可以移动
    back() 或forward() 在您的会话历史记录中。
  • navigate() 比 get() 更快,因为 navigate() 不等待
    页面完全加载。

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.

public class FirefoxInvoke {
                @Test
                public static void browserInvoke()
                {
                    System.setProperty("webdriver.gecko.driver", "gecko-driver-path");
                WebDriver driver=new FirefoxDriver();
                System.out.println("Before"+driver.getTitle());
                driver.get("http://www.google.com");
                driver.get("http://www.facebook.com");
                driver.get("http://www.india.com");
                driver.navigate().back();
                driver.quit();
                }

                public static void main(String[] args) {
                    // TODO Auto-generated method stub
            browserInvoke();
                }

            }

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

public class FirefoxInvoke {
                @Test
                public static void browserInvoke()
                {
                    System.setProperty("webdriver.gecko.driver", "gecko-driver-path");
                WebDriver driver=new FirefoxDriver();
                System.out.println("Before"+driver.getTitle());
                driver.navigate().to("http://www.google.com");
                driver.navigate().to("http://www.facebook.com");
                driver.navigate().to("http://www.india.com");
                driver.navigate().back();
                driver.quit();
                }

                public static void main(String[] args) {
                    // TODO Auto-generated method stub
            browserInvoke();
                }

            }
  • So the main difference between get() and navigate() is, both are
    performing the same task but with the use of navigate() you can move
    back() or forward() in your session's history.
  • navigate() is faster than get() because navigate() does not wait for
    the page to load fully or completely.
半岛未凉 2024-11-08 07:24:10

driver.get(url)navigate.to(url) 均用于转到特定网页。关键的区别在于
driver.get(url):它不维护浏览器历史记录和cookie,并等待页面完全加载。
driver.navigate.to(url):它也用于转到特定网页。它维护浏览器历史记录和 cookie,并且不会等到页面完全加载并在页面之间向后、向前导航并刷新。

driver.get(url) and navigate.to(url) both are used to go to particular web page. The key difference is that
driver.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.

在巴黎塔顶看东京樱花 2024-11-08 07:24:10

为了更好地理解它,我们必须了解 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. Whereas driver.navigate() method sends Post request to Selenium Server Standalone.

辞取 2024-11-08 07:24:09

导航

使用 WebDriver 要做的第一件事就是导航到页面。执行此操作的正常方法是调用 get

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

WebDriver 将等到页面完全加载(即,onload 事件已触发),然后再将控制权返回给您的测试或脚本。值得注意的是,如果您的页面在加载时使用大量 AJAX,那么 WebDriver 可能不知道它何时已完全加载。如果您需要确保此类页面完全加载,那么您可以使用waits

导航:历史记录和位置

之前,我们介绍了如何使用 get 命令 (driver.get("http://www.example.com")) 导航到页面我们已经看到,WebDriver 有许多较小的、以任务为中心的界面,并且导航是一项有用的任务。因为加载页面是一项基本要求,所以执行此操作的方法位于主 WebDriver 接口上,但它只是以下内容的同义词:

driver.navigate().to("http://www.example.com");

重申一下:navigate().to()get() 执行完全相同的操作。一个比另一个更容易打字!

navigate 界面还提供了在浏览器历史记录中前后移动的功能:

driver.navigate().forward();
driver.navigate().back();

(已添加强调)

Navigating

The first thing you’ll want to do with WebDriver is navigate to a page. The normal way to do this is by calling get:

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

WebDriver will wait until the page has fully loaded (that is, the onload event has fired) before returning control to your test or script. It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. If you need to ensure such pages are fully loaded then you can use waits.

Navigation: History and Location

Earlier, we covered navigating to a page using the get command (driver.get("http://www.example.com")) As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. Because loading a page is such a fundamental requirement, the method to do this lives on the main WebDriver interface, but it’s simply a synonym to:

driver.navigate().to("http://www.example.com");

To reiterate: navigate().to() and get() do exactly the same thing. One's just a lot easier to type than the other!

The navigate interface also exposes the ability to move backwards and forwards in your browser’s history:

driver.navigate().forward();
driver.navigate().back();

(Emphasis added)

倾城泪 2024-11-08 07:24:09

他们似乎都导航到给定的网页并引用 @matt 答案:

navigate().to()get() 执行完全相同的操作。

单页应用程序是一个例外。

这两种方法之间的区别不在于它们的行为,而在于应用程序的工作方式以及浏览器处理它的方式的行为。

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:

navigate().to() and get() do exactly the same thing.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文