27

I essentially have a start_url that has my javascript search form and button, hence the need of selenium. I use selenium to select the appropriate items in my select box objects, and click the search button. The following page, I do some scrapy magic. However, now I want to go BACK to the original start_url and fill out a different object, etc. and repeat until no more.

Essentially, I have tried making a for-loop and trying to get the browser to go back to the original response.url, but somehow it crashed. I may try having a duplicate list of start_url's on the top for scrapy to parse through, but I'm not sure if that is the best approach. What can I do in my situation?

petermaxstack
  • 325
  • 1
  • 3
  • 10
  • 1
    is it possible to just use driver.get("url of start page") ? or do you need a way back the way a user would do it? – Dude May 19 '15 at 06:19
  • I actually could just do that! I'm going to try that and the driver.back() method, both would answer my question. – petermaxstack May 19 '15 at 21:33

3 Answers3

34

Here the advice is to use driver.back() : https://selenium-python.readthedocs.io/navigating.html#navigation-history-and-location

Samsul Islam
  • 2,581
  • 2
  • 17
  • 23
seba
  • 399
  • 4
  • 5
  • Thanks! This is exactly it! Based on my question, do you suggest a more efficient way to loop through all the entries, or would the best way to be simply go backwards? – petermaxstack May 19 '15 at 21:32
  • This is a dead link. Please include the relevant content in your answer. – Jacob Peddicord Jan 20 '17 at 23:27
  • since @seba didn't update his answer (dead link). You can find the command in this page section 3.6 : http://selenium-python.readthedocs.io/navigating.html#navigation-history-and-location – nidabdella Aug 23 '17 at 08:21
19

The currently selected answer provides a link to an external site and that link is broken. The selenium docs talk about

driver.forward()
driver.back()

but those will sometimes fail, even if you explicitly use some wait functions.

I found a better solution. You can use the below command to navigate backwards.

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

hope this helps someone else in the future.

user1610950
  • 1,837
  • 5
  • 33
  • 49
  • 1
    I found the "window.history.go(-2)" command helpful for those weird websites that just refresh when you click the back button instead of actually going back. – Jed Jul 09 '18 at 18:22
  • Thanks @user1610950 I see that the script `driver.execute_script("window.history.go(+1)")` happily navigates forwards reliably for me too. –  Jul 27 '20 at 18:19
3

To move backwards and forwards in your browser’s history use

driver.forward()
driver.back()
Shaik
  • 400
  • 3
  • 9