Skip to main content

You are here

Navigation using the browser Back or Front button

The following code programmatically clicks on the browser's "Front" or "Back" navigation buttons to move between visited pages. Between each navigation is a brief waiting period to slow the process down so its noticeable.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Navigation {
public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

// navigate to home page of yahoo
driver.get("http://uk.yahoo.com/"); 

//navigate to another page - yahoo news
    driver.findElement(By.xpath("//*[@id='pa-u_25021262-bd']/a/span[2]")).click();  //this moves to a next page. So you can now use browser back button
    Thread.sleep(8000L);
   
    //go back to home page after a delay using the browser Back Button
    driver.navigate().back();
   
//navigate again to the yahoo news page using the Forward button now that its exists from previous navigation operation
    driver.navigate().forward();
    Thread.sleep(8000L);
   
    //go back to home page after a delay using the browser Back Button
    driver.navigate().back();
    Thread.sleep(8000L);
   
    driver.quit(); //DONE. Now close the browser.
}
}

Category: 

Language: 

0
No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.

Disclaimer

Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes