Python selenium 浏览器 driver.back()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27626783/
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
Python selenium browser driver.back()
提问by BubblewrapBeast
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.
我创建了一个小屏幕抓取器,一切似乎都运行良好,信息正在被提取并保存在数据库中。我遇到的唯一问题是有时 Python 不使用 ,driver.back()
因此它会尝试获取错误页面上的信息并崩溃。我试过添加一个,time.sleep(5)
但有时它仍然不起作用。我正在尝试优化它以尽可能减少时间。所以让它休眠 30 秒似乎不是一个好的解决方案。
采纳答案by Peanut Frogman
This is the best solution. The back() and forward() methods aren't guaranteed to work.
这是最好的解决方案。back() 和 forward() 方法不能保证工作。
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.
传入的 JavaScript 访问页面 Dom 以导航到上一个 url。我希望这能解决你的问题。