如何将Python的Selenium WebDriver中的所有cookie保存到txt文件中,然后再加载它们?该文档没有说明getCookies函数的任何内容.
为了保存 chromedriver 会话,我使用了以下代码片段:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('user-data-dir= path to where to save session')
driver = webdriver.Chrome(executable_path='path to chromedriver.exe', chrome_options=options)
Run Code Online (Sandbox Code Playgroud)
我尝试用 Firefox 做同样的事情,但似乎不起作用:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument('user-data-dir= path to where to save session')
driver = webdriver.Firefox(executable_path='path to geckodriver.exe', firefox_options=options)
Run Code Online (Sandbox Code Playgroud)
这是正确的方法还是我错过了什么?
我知道以前有人问过这个问题……但我尝试了多种方法,出于某种原因,我从驱动程序下载的任何内容都会进入我的下载文件夹。
基本上我导航到一个网站并通过点击下载链接下载一些东西,如下所示:
result.click()
Run Code Online (Sandbox Code Playgroud)
这可以很好地下载文件。但我想将其下载到特定目录。我尝试使用这些方法来更改下载目录:
driver = webdriver.Firefox()
profile = webdriver.FirefoxProfile()
driver.command_executor._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
driver.execute("SET_CONTEXT", {"context": "chrome"})
driver.execute_script("""
Services.prefs.setBoolPref('browser.download.useDownloadDir', true);
Services.prefs.setStringPref('browser.download.dir', arguments[0]);
""", directory)
driver.execute("SET_CONTEXT", {"context": "content"})
Run Code Online (Sandbox Code Playgroud)
和
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", directory)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
Run Code Online (Sandbox Code Playgroud)
directory我想要的位置在哪里。
这些都不起作用......谁能解释为什么或告诉我如何实际实现这一目标?
谢谢