Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python selenium browser driver.back()

I have created a little screen scraper and everything seems to be working great, the information is being pulled and saved in a db. The only problem I am having is sometimes Python doesn't use the driver.back() so it then trys to get the information on the wrong page and crashes. I have tried adding a time.sleep(5) but sometimes it still isn't working. I am trying to optimise it to take as little time as possible. So making it sleep for 30 seconds doesn't seem to be a good solution.

like image 575
BubblewrapBeast Avatar asked Dec 23 '14 19:12

BubblewrapBeast


People also ask

How do I go back to browser using Selenium?

navigate(). back(); It is just like pressing the browser's back button. In WebDriver, this method enables the web browser to click on the back button in the existing browser window.

How do I go back and forward in Selenium?

The respective command that takes you forward by one page on the browser's history can be written as: driver. navigate(). forward();

What is return in Selenium?

We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys. ENTER as an argument to the method. Also, we can use pass Keys.


1 Answers

This is the best solution. The back() and forward() methods aren't guaranteed to work.

 driver.execute_script("window.history.go(-1)") 

The JavaScript passed in accesses the pages Dom to navigate to the previous url. I hope that this solves your problem.

like image 83
Peanut Frogman Avatar answered Sep 19 '22 09:09

Peanut Frogman