【问题标题】:Save and load browser history for selenium保存和加载 selenium 的浏览器历史记录
【发布时间】:2014-03-14 20:05:14
【问题描述】:

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

【问题讨论】:

    标签: python selenium browser-history


    【解决方案1】:

    您对此无能为力。首先,selenium webdriver API 只为您提供了forward()back() 用于浏览历史记录的方法(documentation)。

    DOM window 对象可以帮助您,因为它提供了对 history 对象的访问,但出于安全原因:you cannot push history items from outside of domain of the current page

    另见:

    但是,如果您的所有网址都在同一个域中,您可以使用history.pushState() 方法,例如:

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

    这里,在火狐浏览器中打开维基百科主页,然后插入一个历史项目,然后再次打开主页。如果您在浏览器窗口中单击Back,您将获得“我是如何认识你母亲的”维基百科页面。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 2015-06-14
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多