Navigate

Navigation commands are some which enables the user to navigate to some webpages and also to work with history like back, forth, refresh, to. We can access the these method by accessing the navigate() method present in the driver (FF,IE,GC,Safari) class Methods we can access through navigate() method are below:

    to(String string)
    to(Url url) //overloaded to() method
    back()
    forward()
    refresh()
to(String value);

to(string) method navigates the user to the particular webpage without using its protocol unlike get(“᾿) method

Optimized code for to(String value)


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.WebDriver.Navigation;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class To 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://chercher.tech");
								driver.navigate().to("http://gmail.com");					
							}
						}

					

Elaborated code for to(String value)


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.WebDriver.Navigation;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class To 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://chercher.tech");
								Navigation nav = driver.navigate();
								nav.to("http://gmail.com");			
							}
						}


					

Elaborated code for to(URL url)


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.WebDriver.Navigation;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class To 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://selenium-mentor.com");
								driver.navigate().to(new URL(http://gmail.com));
							}
						}



					

Multi-level Inheritance in Java and Selenium

back();

back() method helps the user to navigate back.

Optimized code for navigate back()


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.WebDriver.Navigation;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class Back 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://selenium-mentor.com");
								driver.navigate().back();							
							}
						}


					

In above program you might not face any error but it also will not navigate anywhere ,why ? because back() calls the back() native method present in the browser indirectly so it works only when you navigate more than one page.Then only the button on the browser will be enabled unless it enabled the back() method never works.

Modify the above program like below one


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.WebDriver.Navigation;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class Back 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://selenium-mentor.com");
								driver.get("http://google.com");
								driver.navigate().back();							
							}
						}
						

forward ()

The forward() method navigates the user to forth but if we want forward() method to work we should use back() before using forward() then only the forward button in browser or it creates the place in browser history Syntax:driver.navigate().forward();


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class Forward 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://chercher.tech");
								driver.get("http://google.com");
								driver.navigate().back();
								driver.navigate().forward();							
							}
						}



					

refresh()

refresh() method refreshes the current webpage not suggested for https pages


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class Refresh 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();		
								driver.get("https://chercher.tech");		
								driver.navigate().refresh();		
							}
						}


					

Different ways to refresh a page


1.Using sendKeys.Keys method
driver.get("https://chercher.tech");
driver.findElement(By.tageName("body")).sendKeys(Keys.F5);

2.Using navigate.refresh() method
driver.get("https://chercher.tech");
driver.navigate().refresh();

3.Using navigate.to() method
driver.get("https://chercher.tech");
driver.navigate().to(driver.getCurrentUrl());

4.Using get() method
driver.get("https://chercher.tech");
driver.get(driver.getCurrentUrl());

5.Using sendKeys() method using keyboard
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech