سؤال

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.

هل كانت مفيدة؟

المحلول

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).

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.

See also:

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')

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.

Hope that helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top