【问题标题】:How to load firefox profile with Python Selenium?如何使用 Python Selenium 加载 Firefox 配置文件?
【发布时间】:2018-10-23 13:55:26
【问题描述】:

我正在尝试让 Python Selenium 在我的 Windows 机器上运行。我已升级到最新版本的 Firefox、Selenium、Geckodriver,但仍然收到以下错误:

Python 脚本

from selenium import webdriver
driver = webdriver.Firefox()

错误

Traceback (most recent call last):
  File "run.py", line 17605, in <module>
  File "<string>", line 21, in <module>
  File "site-packages\selenium\webdriver\firefox\webdriver.py", line 77, in __init__
  File "site-packages\selenium\webdriver\firefox\extension_connection.py", line 49, in __init__
  File "site-packages\selenium\webdriver\firefox\firefox_binary.py", line 68, in launch_browser
  File "site-packages\selenium\webdriver\firefox\firefox_binary.py", line 103, in _wait_until_connectable
WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.

我也尝试使用以下代码创建 Firefox 配置文件:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
profile.set_preference('general.warnOnAboutConfig', False)

gecko_path = "path_to_geckodriver\\geckodriver.exe"
path = "path_to_firefoxs\\Mozilla Firefox\\firefox.exe"
binary = FirefoxBinary(path)
driver = webdriver.Firefox(firefox_profile=profile,executable_path=gecko_path)
  • Python 2.7
  • 火狐60
  • Geckodriver-v0.20.1-win64.zip
  • 硒 3.12.0

【问题讨论】:

标签: python selenium firefox firefox-profile


【解决方案1】:

解决方案:

from selenium import webdriver
fp = webdriver.FirefoxProfile('/home/gabriel/.mozilla/firefox/whatever.selenium')
driver = webdriver.Firefox(fp)

我一直在努力,直到找到 this issue,它现在已解决,但很有帮助,因为它显示了一些命令。

第一个版本,因为后来我无法连接硒而无法工作:

from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = Options()
options.add_argument("-profile")
options.add_argument("/home/gabriel/.mozilla/firefox/whatever.selenium")
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_options=options)

我确信这会加载配置文件“whatever.selenium”,因为如果我转到 about:profiles 我可以阅读:

简介:硒 这是正在使用的配置文件,无法删除。

即使“whatever.selenium”不是我系统上的默认配置文件。

备注:至少有一个配置文件参数被 selenium(或 geckodriver?)覆盖:首选项 > 隐私和安全 > “阻止弹出窗口”始终重置为关闭。因此,请使用 about:profiles 对您正在运行的配置文件进行断言。

注释:

  • 上面的代码中可能不需要firefox_capabilities
  • 在 Firefox 60.4.0esr(64 位)、geckodriver 0.23.0 (2018-10-04)、selenium 3.141.0 和 Python 3.5.3 下测试

【讨论】:

  • 所以你这里有两个代码块,第一个应该是解决方案,但它不起作用,因为你没有在任何地方指定驱动程序位置。你知道.. 壁虎司机?
  • 请忽略第二段代码。在我的代码中,创建驱动程序的行是:driver = webdriver.Firefox(fp, options=options, executable_path="path-to-geckodriver")
【解决方案2】:

您没有更新个人资料的偏好设置:profile.update_preferences()。 您必须在设置首选项后更新配置文件。请按照以下代码:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
profile.set_preference('general.warnOnAboutConfig', False)
profile.update_preferences()
gecko_path = "path_to_geckodriver\\geckodriver.exe"
path = "path_to_firefoxs\\Mozilla Firefox\\firefox.exe"
binary = FirefoxBinary(path)
driver = webdriver.Firefox(firefox_profile=profile,executable_path=gecko_path)

【讨论】:

    【解决方案3】:

    在我使用的窗口上:

    fp = webdriver.FirefoxProfile('C:/Users/x/AppData/Roaming/Mozilla/Firefox/Profiles/some-long-string')
    driver = webdriver.Firefox(firefox_profile=fp)
    ...
    

    1 - 要查找当前的 Profile Folder,请在 url 字段中输入 about:support,然后按 Enter。
    2 - 要查看所有用户配置文件,请在 url 字段中键入 about:profiles,然后按 Enter。

    【讨论】:

    • 这对我有用,但请注意,使用“根目录”而不是本地目录。我一直在尝试使用本地目录,但这从来没有奏效。一旦我切换到根目录,它就可以完美运行。您可以通过打开 Firefox 并在浏览器中输入“about:profiles”来找到您的根目录和本地目录位置。
    【解决方案4】:

    切换到 chrome 驱动程序。 Firefox、gecko 和 selenium 现在不能很好地协同工作。这是最终对我有用的方法。

    import unittest
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    
    class TestTemplate(unittest.TestCase):
        """Include test cases on a given url"""
    
        def setUp(self):
            """Start web driver"""
            chrome_options = webdriver.ChromeOptions()
            chrome_options.add_argument('--no-sandbox')
            self.driver = webdriver.Chrome(chrome_options=chrome_options)
            self.driver.implicitly_wait(10)
    
        def tearDown(self):
            """Stop web driver"""
            self.driver.quit()
    
        def test_case_1(self):
            """Go to python.org and print title"""
            try:
                self.driver.get('https://www.python.org/')
                title = self.driver.title
                print title
            except NoSuchElementException as ex:
                self.fail(ex.msg)
    
        def test_case_2(self):
            """Go to redbull.com and print title"""
            try:
                self.driver.get('https://www.redbull.com')
                title = self.driver.title
                print title
            except NoSuchElementException as ex:
                self.fail(ex.msg)
    
    if __name__ == '__main__':
        suite = unittest.TestLoader().loadTestsFromTestCase(TestTemplate)
        unittest.TextTestRunner(verbosity=2).run(suite)
    

    我使用加载帧缓冲区的 Jenkinsfile 来调用 selenium 和 python 脚本。

    您可以轻松地在本地计算机上运行它。你可能想要一个与 linux 一起使用的 vagrant box。

    这是启动 python 脚本的内容。

    sh "(Xvfb :99 -screen 0 1366x768x16 &) && (python ./${PYTHON_SCRIPT_FILE})"

    这是从运行此 Docker 映像的 docker 文件中调用的。

    https://github.com/cloudbees/java-build-tools-dockerfile

    【讨论】:

      【解决方案5】:

      在 java 中加载自定义的 firefox 配置文件:

      FirefoxOptions options = new FirefoxOptions();
      
      ProfilesIni allProfiles = new ProfilesIni();         
      FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile"); // manualy created profile in firefox profile manager
      options.setProfile(selenium_profile);
      
      options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
      System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
      driver = new FirefoxDriver(options);
      driver.manage().window().maximize();
      

      【讨论】:

        猜你喜欢
        • 2016-06-25
        • 2016-03-23
        • 2018-04-27
        • 1970-01-01
        • 2017-12-02
        • 2018-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多