Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For scrapy/selenium is there a way to go back to a previous page?

Tags:

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?

like image 745
petermaxstack Avatar asked May 19 '15 03:05

petermaxstack


People also ask

How do I go back to previous page in Selenium?

We can also move back in the browser with the help of a Javascript Executor in Selenium. It has the execute_script() method which allows Selenium to run Javascript commands. We have to execute the Javascript command window. history.go(-1) to go back to the previous page.

Can we move back and forward in browser using Selenium?

New Selenium IDEWe can move backward and forward in browser history with the help of the Selenium webdriver with Python. To navigate a step forward in history the method forward is used. To navigate a step backward in history the method back is used.


2 Answers

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

like image 174
seba Avatar answered Sep 27 '22 22:09

seba


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.

like image 35
user1610950 Avatar answered Sep 27 '22 22:09

user1610950