python - 如何使用 Selenium Webdriver 就地使用现有配置文件?
问题描述
我正在尝试使用 Python 的Selenium Webdriver做标题所说的事情,现有的Firefox
配置文件位于<PROFILE-DIR>
.
我试过的
- 在驱动程序上使用options.profile选项:
#!/usr/bin/env python
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver import Firefox, DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
options = Options()
options.profile = '<PROFILE_DIR>'
webdriver = Firefox(options=options)
这会将现有配置文件复制到临时位置。我可以看到它有效,因为我开始的新会话可以访问配置文件的旧 cookie 等。但这不是我所追求的:我想就地使用配置文件。
- 试图将配置文件作为“--profile”传递给args 功能:将上面的代码更改为
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['args'] = '--profile <PROFILE-DIR>'
webdriver = Firefox(desired_capabilities=capabilities)
什么都不做:geckodriver.log
在关闭会话后查看仍然向我显示类似的Running command: "/usr/bin/firefox" "--marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileOFKY46"
内容,即仍在使用临时配置文件(甚至不是现有配置文件的副本;旧的 cookie 不存在)。
尝试了相同的
capabilities['args'] = ['-profile', '<PROFILE-DIR>']
方法(即字符串列表而不是单个字符串);同样的结果。阅读了一堆其他的SO帖子,但都没有。这主要是因为它们特定于
Chrome
(您显然可以将命令行选项传递给其驱动程序;我还没有看到类似的东西geckodriver
),或者因为它们回退到现有配置文件的副本。
在这个方向上最相关的答案在紧要关头实现了我想到的基本相同的黑客:
options.profile
使用如上所述的现有配置文件的副本启动驱动程序;完成后手动关闭驱动程序实例(例如使用
Ctrl+C
, 或SIGINT
)以便临时配置文件目录不会被删除;复制留在现有配置文件顶部的所有内容,让您可以访问自动会话中所需的任何剩余内容。
这很丑陋,感觉没有必要。此外,geckodriver
未能删除临时配置文件(我将依赖它)被认为是一个错误..
当然,我误解了如何通过上面提到的那些功能选项,或者类似的东西。但是文档可以更好地提供示例。
解决方案
推荐阅读
- java - 作为消费者参数的 Lambda 表达式无法正常工作
- fortran - 独立调用 fortran 子程序,在参数中没有定义
- r - 列表中的字符串是否存在于列中?
- c++ - 如何在opengl Tessalation评估着色器中插入矩阵
- ms-access - 如何控制访问表单上命令按钮的位置?
- xero-api - 过帐发票时的 xero API 如何添加空单位金额和数量?
- csv - 如何将多个csv文件与一个具有相同标题的文件合并
- ruby-on-rails - 未知的嵌套变量无法确定嵌套子的存在......事物,总是 NoMethodError
- arrays - APEX_JSON.get_count 无法识别 JSON 路径
- snakemake - Snakemake 总是重建目标,即使是最新的