如何使用Python Selenium加载firefox配置文件? [英] How to load firefox profile with Python Selenium?

查看:375
本文介绍了如何使用Python Selenium加载firefox配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get Python Selenium to work on my Windows Machine. I've upgraded to the latest versions of Firefox, Selenium, Geckodriver, but I still receive the below error:

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配置文件:

I've also tried creating the firefox profile with the below code:

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
  • Firefox 60
  • Geckodriver-v0.20.1-win64.zip
  • 硒3.12.0
    • Python 2.7
    • Firefox 60
    • Geckodriver-v0.20.1-win64.zip
    • Selenium 3.12.0
    • 推荐答案

      解决方案:

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

      我一直努力直到找到此问题,该问题现已解决,但由于有帮助,它显示了一些命令.

      I struggled until I found this issue which is now solved but was helpful because it shows some commands.

      第一个版本,无法正常工作,因为之后无法连接硒:

      first version, not working because I could not connect with selenium afterward:

      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,我会读到:

      I am sure that this loads the profile "whatever.selenium" because if I go to about:profiles I can read:

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

      Profile: selenium This is the profile in use and it cannot be deleted.

      即使"whatever.selenium"不是系统上的默认配置文件.

      Even though "whatever.selenium" is not the default profile on my system.

      备注:硒(或geckodriver?)至少覆盖了一个配置文件参数:首选项>隐私和安全性>阻止弹出窗口"始终重置为关闭状态.因此,使用about:profiles可以断言您正在运行的配置文件.

      注释:

        上面的代码中可能不需要
      • firefox_capabilities.
      • 在Firefox 60.4.0esr(64位),geckodriver 0.23.0(2018-10-04),硒3.141.0和Python 3.5.3下进行了测试
      • firefox_capabilities might not be needed in above code.
      • tested under Firefox 60.4.0esr (64-bit), geckodriver 0.23.0 ( 2018-10-04), selenium 3.141.0 with Python 3.5.3

      这篇关于如何使用Python Selenium加载firefox配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆