How to Go Back or Forward in a Web Browser using the Selenium Module in Python



Python


In this article, we show how to go back or forward in a web browser using the Selenium module in Python.

Going back allows us to go to the previous page we were at before. Going forward allows us to go forward to the page we were at before we clicked the back button.

All web browsers have back and forward buttons.

So the selenium module has built-in functions which allow us to go back and forward. These are the functions, back() and forward()

This is shown in the code below.



So by using the back() function, we are able to go back to the previous page.

By using the forward() function, we are able to go forward to the previous page.

Below we have an example of full code which opens the Firefox web browser, goes to the walmart web page and then goes to the amazon web page. The browser then goes back to the walmart web page and then goes forward back to the amazon web page.

This is shown in the code below.





So we have code that opens the Firefox web browser.

We go to walmart's home page first and then go to amazon's home page.

We then have the line, browser.back(), which goes back to the walmart page.

We then have the line, browser.forward(), which goes forward back to the amazon page.

So this simulates exactly how back and forward buttons work in a web browser.

And this his how we can go back or forward in a web browser using the selenium module in Python.


Related Resources

How to Create a Zip File in Python

How to Extract All Files and Folders from a Zip File in Python

How to Read the Contents of a Zip File in Python



HTML Comment Box is loading comments...