Python 对于scrapy/selenium,有没有办法返回上一页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30316448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
For scrapy/selenium is there a way to go back to a previous page?
提问by petermaxstack
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.
我基本上有一个 start_url 有我的 javascript 搜索表单和按钮,因此需要 selenium。我使用 selenium 在我的选择框对象中选择适当的项目,然后单击搜索按钮。在接下来的页面中,我做了一些爬虫魔术。但是,现在我想回到原来的 start_url 并填写一个不同的对象等,然后重复直到不再。
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?
从本质上讲,我尝试进行 for 循环并尝试让浏览器返回原始 response.url,但不知何故它崩溃了。我可能会尝试在顶部有一个重复的 start_url 列表,以便scrapy 解析,但我不确定这是否是最好的方法。在我的情况下我能做什么?
采纳答案by seba
Here the advice is to use driver.back(): https://selenium-python.readthedocs.io/navigating.html#navigation-history-and-location
这里的建议是使用driver.back():https: //selenium-python.readthedocs.io/navigating.html#navigation-history-and-location
回答by Shaik
To move backwards and forwards in your browser's history use
要在浏览器的历史记录中前后移动,请使用
driver.forward()
driver.back()
回答by user1610950
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.
希望这可以帮助其他人在未来。