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();
WebElement searchBox = driver.findElement("q"));
WebElement SearchButton = driver.findElement("btng"));;
searchBox.sendKeys("Again WebDriver");;
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.