如何使用Selenium Python geckodriver启动Firefox特定配置文件

7

以下是我的代码:

profile = webdriver.FirefoxProfile('C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\kvycjolb.Prdel')
driver = webdriver.Firefox(profile)

我没有遇到任何错误,火狐浏览器也能够启动,但是使用这个配置文件时它却无法加载:我尝试过将 / 改为 // 等等,但是都没有成功。

以下方法也无效:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
profile = FirefoxProfile("C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\kvycjolb.Prdel")
driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=binary, executable_path="C:\\aprog\\geckodriver.exe")
driver.get('https://google.com')

我收到了错误信息:
C:\aprog>testff
Traceback (most recent call last):
  File "C:\aprog\testff.py", line 7, in <module>
    driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=binary, e
xecutable_path="C:\\aprog\\geckodriver.exe")
  File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py",
line 152, in __init__
    keep_alive=True)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 256, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unable to find a matchin
g set of capabilities
4个回答

6
我认为官方答案可以在文档中找到。

目前如下:

# Custom profile folder to keep the minidump files
profile = tempfile.mkdtemp(".selenium")
print("*** Using profile: {}".format(profile))

# Use the above folder as custom profile
opts = Options()
opts.add_argument("-profile")
opts.add_argument(profile)
opts.binary = "/Applications/Firefox.app/Contents/MacOS/firefox"

driver = webdriver.Firefox(options=opts,
    # hard-code the Marionette port so geckodriver can connect
    service_args=["--marionette-port", "2828"])

我得到了“未定义名称'Options'” - Mattman85208
1
@MatthewCox,你是否按照文档使用了 from selenium.webdriver.firefox.options import Options - akostadinov

3
def setFirefoxDriver():
    profilePath = r"PathHere" 
    driverPath  = r"pathHere\driver.exe"

    options = Options()
    options.add_argument("-profile")
    options.add_argument(profilePath)

    dService = Service(driverPath)
    d = webdriver.Firefox(service=dService, options=options)

    return d

d = setFirefoxProfile()

d.get('https://www.amazon.com/)
  • 要查找您的Firefox配置文件路径,请在地址栏中输入about:supportabout:profiles

  • 您可以加载自己的配置文件进行测试,查看是否可以载入Cookie,例如:当我访问amazon.com时,Amazon会识别出我。

  • 请注意,您不能在两个不同的实例中使用相同的配置文件,因此如果您想要加载您的配置文件用于Selenium测试,则不应该使用Firefox的默认配置文件,而应该使用另一个配置文件。


2
要通过 Selenium 3.4.3geckodriver v0.18.0Mozila Firefox 53.0Python 3.6 使用特定的 Firefox 配置文件启动 Mozilla Firefox,您需要根据文档 here 使用 Firefox Profile Manager 创建一个单独的 Firefox Profile
我创建了一个名为 debanjan 的 Firefox 配置文件。该配置文件存储在以下子目录中: "C:\Users\AtechM_03\AppData\Roaming\Mozilla\Firefox\Profiles" 配置文件(文件夹)的名称为 w8iy627a.debanjan。因此,在初始化 WebDriver 实例时,我们必须传递命名为 w8iy627a.debanjan 的 Firefox 配置文件的绝对路径,如下所示:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
profile = FirefoxProfile("C:\\Users\\AtechM_03\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\w8iy627a.debanjan")
driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get('https://google.com')

请告诉我这是否回答了你的问题。

我使用Python27,出现错误:无法找到匹配的能力集。 - user3281831
请问您能否在问题区更新您的代码和错误信息以供进一步分析?谢谢。 - undetected Selenium
Selenium,GeckoDriver,Mozilla Firefox 版本,请。谢谢。 - undetected Selenium
Firefox 54.0.1,Geckodriver 0.18.0win64 - user3281831
Firefox 54.0.1,Geckodriver 0.18.0win64 selenium 3.4.3 - user3281831

0

在路径中始终使用双反斜杠(至少对于Windows路径而言):

profile = webdriver.FirefoxProfile('C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\kvycjolb.Prree')

在你的代码中,你同时使用了反斜杠和正斜杠。

我已经在代码中修复了它,但它并没有解决问题。 - user3281831

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接