Selenium WebDriver Navigation Commands
Navigation Commands:
Given are some of the most used navigation commands.
Command
driver.navigate().forward();
This method enables the web browser to click on the forward button in the existing browser window. It neither accepts anything nor returns anything.
Command
driver.navigate().back();
this method enables the web browser to click on the back button in the existing browser window. It neither accepts anything nor returns anything.
Command
driver.navigate().to("URL");
This method loads a new web page in the existing browser window. It accepts String as parameter and returns void.
Command
driver.navigate().refresh();
this method refresh/reloads the current web page in the existing browser window. It neither accepts anything nor returns anything.
CODE
package pkg1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Navigation_Commands {
public static void main(String[] args) {
//System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Files\\chromedriver.exe");
//Instantiate a ChromeDriver class.
WebDriver driver = new ChromeDriver();
//Maximize the browser
driver.manage().window().maximize();
// Launch Website
driver.navigate().to("https://www.bookofnetwork.com/automation-testing/Automation-Testing-With-Selenium");
//Click on the Link Text using click() command
driver.findElement(By.xpath("//a[text()=`2. Navigation Commands`]")).click();
//Go back to Home Page
driver.navigate().back();
//Go forward to Registration page
driver.navigate().forward();
//Refresh browser
driver.navigate().refresh();
//Closing browser
driver.close();
}
}