Python selenium browser driver.back()

Question:

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.

Asked By: BubblewrapBeast

||

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.

Answered By: Peanut Frogman

Try relocating elements each time it goes back to previous page. That will surely work but it is time consuming.

Answered By: Kidus
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.