Wednesday, September 16, 2015

WebDriver Browser Navigation Commands

WebDriver Browser Navigation Commands


navigate().back(): Navigates to back page: you use this command to go back by one page on the browser's history.

Let's take an example:
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("WebDriver");
WebElement SearchButton = driver.findElement(By.name("btng"));
searchButton.click();
searchBox.clear();
searchBox.sendKeys("Again WebDriver");
searchButton.click();
driver.navigate().back();
The code opens the Google search page, and firstly it searches for the text "WebDriver"; then, after the search results are loaded, it does a second search for "Again WebDriver" and waits for the results. Now as the navigation history is created in the browser, it uses webDriver navigation to go back in the browser history.

A worth reading example of navigate().back() Navigate to previous page using Selenium.

Similarly, you can use navigate().forward(): It navigates to forward page: you use this command to go forward by one page in the browser's history.

In the above code you have also seen navigate().to() which opens a new browser window.

driver.navigate().refresh(): Refreshes the page.