首页 > 解决方案 > 如何使用 Selenium Webdriver 就地使用现有配置文件?

问题描述

我正在尝试使用 Python 的Selenium Webdriver做标题所说的事情,现有的Firefox配置文件位于<PROFILE-DIR>.

我试过的

#!/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 等。但这不是我所追求的:我想就地使用配置文件。

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 不存在)。

在这个方向上最相关的答案在紧要关头实现了我想到的基本相同的黑客:

  1. options.profile使用如上所述的现有配置文件的副本启动驱动程序;

  2. 完成后手动关闭驱动程序实例(例如使用Ctrl+C, 或SIGINT)以便临时配置文件目录不会被删除;

  3. 复制留在现有配置文件顶部的所有内容,让您可以访问自动会话中所需的任何剩余内容。

这很丑陋,感觉没有必要。此外,geckodriver未能删除临时配置文件(我将依赖它)被认为是一个错误..

当然,我误解了如何通过上面提到的那些功能选项,或者类似的东西。但是文档可以更好地提供示例。

标签: pythonseleniumselenium-webdriverfirefox

解决方案


推荐阅读