Navigation Commands in Selenium :
- Using Selenium Navigation Commands able open a web page URL, navigate to other web page by clicking any element, Back, Forward or Refresh the web page using browser’s history.
- Using the navigating interface is able to move back and forward in browser history.
Navigate To Command:
- “navigate().to()” : This command is like the get() command.
- It opens a new browser window and it will receive / find the page you provided.
- Able to redirect from the current web page to the expected web page.
1 2 3 | // Using navigate to command navigate to another url driver.navigate().to("https://stqatools.com/"); |
Forward Command:
- “navigate().forward()” : A page leads you to the history of the browser.
- Navigate (). Move forward page using the Forward () command.
1 2 3 | // Using Forward command navigate to forward page driver.navigate().forward(); |
Back Command:
- “navigate().back()” : This command is used to return to a page in the browser’s history.
- Navigate (). Use the back () command to go to the backward page.
1 2 3 | // Using Back command navigate to back page driver.navigate().back(); |
Refresh Command:
- “navigate().refresh()” : This command is used to refresh the current page.
- Refresh the current page if there is a change in the web page at times.
1 2 3 | // Using Refresh command Refresh current page driver.navigate().refresh(); |
Navigation Commands in Selenium Steps :
- Set geckodriver path to launch browser.
- Create a instance of FirefoxDriver() to handle Navigation commands.
- Navigate to URL.
- Wait until Page Load.
- Navigate to another URL.
- Navigate to back page.
- Navigate to forward page.
- Refresh current page.
Example of Navigation Commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Navigation_Commands { public static void main(String[] args)throws InterruptedException{ // Set geckodriver path System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); // Instantiating objects and variables WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Using navigate to command navigate to another url driver.navigate().to("https://stqatools.com/"); // Using Back command navigate to back page driver.navigate().back(); // Using Forward command navigate to forward page driver.navigate().forward(); // Using Refresh command Refresh current page driver.navigate().refresh(); } } |