保存和加载硒的浏览器历史记录 [英] Save and load browser history for selenium

查看:127
本文介绍了保存和加载硒的浏览器历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Selenium Webdriver for Python,是否可以保存会话的浏览器历史记录并在以后的会话中重新加载历史记录?我知道,如果Cookie与当前域匹配,则可以保存和重新加载Cookie,但是我正在尝试测试配置文件类型不同的网站.谢谢.

Using Selenium Webdriver for Python, is it possible to save the browser history of a session and reload the history in a future session? I know cookies can be saved and reloaded if they match the current domain but I'm trying to test websites with different types of profiles. Thanks.

推荐答案

对此您无能为力.首先,Selenium Webdriver API仅提供用于导航历史记录的 forward() back()方法(

There is not so much you can do about it. First of all, selenium webdriver API provides you with just forward() and back() methods for navigating the history (documentation).

DOM window 对象本可以为您提供帮助,因为它提供了对您不能从当前页面的域外推送历史记录项..

The DOM window object could have helped you since it provides access to the history object, but due to security reasons: you cannot push history items from outside of domain of the current page.

另请参阅:

但是,如果所有URL都在同一个域内,则可以使用 history.pushState()方法,例如:

But, if all of your URLs are within the same domain, you can get use of history.pushState() method, example:

from selenium.webdriver.firefox import webdriver


WIKI_PAGE = 'How_I_Met_Your_Mother'

driver = webdriver.WebDriver()
driver.get('https://en.wikipedia.org/wiki')

script = 'history.pushState({}, "", "%s")' % WIKI_PAGE
driver.execute_script(script)

driver.get('https://en.wikipedia.org/wiki')

在此处,在Firefox浏览器中打开Wikipedia主页,然后插入历史记录项,然后再次打开主页.如果您在浏览器窗口中单击返回,则将获得我如何见到母亲"维基百科页面.

Here, wikipedia main page is opened in Firefox browser, then a history item is inserted, then the main page is opened again. If you'll click Back in the browser window, you'll get "How I met your mother" wikipedia page.

希望有帮助.

这篇关于保存和加载硒的浏览器历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆