【问题标题】:How to start Firefox with with specific profile Selenium Python geckodriver如何使用特定配置文件 Selenium Python geckodriver 启动 Firefox
【发布时间】:2018-01-13 05:32:49
【问题描述】:

这是我的代码:

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

我没有收到任何错误并且 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\\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

【问题讨论】:

    标签: python selenium geckodriver


    【解决方案1】:

    我想官方答案在documentation

    目前是:

    # 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"])
    

    【讨论】:

    • 我得到“未定义的名称'选项'
    • @MatthewCox,您在文档中是否使用了from selenium.webdriver.firefox.options import Options
    【解决方案2】:

    要通过 Selenium 3.4.3geckodriver v0.18.0Mozila Firefox 53.0Python 3.6 使用特定的 Firefox 配置文件启动 Mozilla Firefox,您需要根据文档 here 创建一个单独的 Firefox ProfileFirefox Profile Manager .

    我创建了一个名为 debanjanFirefox Profile。此配置文件存储在此子目录中:

    “C:\Users\AtechM_03\AppData\Roaming\Mozilla\Firefox\Profiles”

    配置文件(文件夹)的名称是 w8iy627a.debanjan。因此,在启动WebDriver 实例时,我们必须传递名为w8iy627a.debanjanFirefox 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\\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,出现错误:无法找到匹配的功能集。
    • 您能否更新问题区域中的代码和错误以进行进一步分析?谢谢
    • 请提供 Selenium、GeckoDriver、Mozilla Firefox 版本。谢谢
    • Firefox 54.0.1, Geckodriver 0.18.0win64
    • Firefox 54.0.1, Geckodriver 0.18.0win64 selenium 3.4.3
    【解决方案3】:

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

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

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

    【讨论】:

    • 我已经在代码中修复了,但没有解决问题。
    猜你喜欢
    • 1970-01-01
    • 2015-08-06
    • 2019-01-11
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2021-08-06
    • 2018-10-23
    • 2018-02-08
    相关资源
    最近更新 更多