python - Selenium 无法打开 Firefox 48.0.1

标签 python selenium firefox

我开始学习如何在 Django 中创建 Web 应用程序时成为更好的测试驱动开发人员。我正在尝试使用 Selenium 打开浏览器,但出现错误。

selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: /var/folders/xn/bvyw0fm97j1_flsyggj0xn9r0000gp/T/tmptoxt890d If you specified a log_file in the FirefoxBinary constructor, check it for details.

我读到“安装 FF 扩展“禁用附加组件兼容性检查”会跳过这个,一切都很好。” selenium.common.exceptions.WebDriverException: Message: Can't load the profile .我这样做了,但它仍然无法正常工作。我将 Python2.7 和 Python3.5 与 Selenium 版本 2.53.6 一起使用。

Python文件

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import unittest

caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True

class NewVisitorTest(unittest.TestCase):  

    def setUp(self):  
        self.browser = webdriver.Firefox(capabilities=caps)

    def tearDown(self):  
        self.browser.quit()

    def test_can_start_a_list_and_retrieve_it_later(self):  
        self.browser.get('http://localhost:8000')

        self.assertIn('To-Do', self.browser.title)  

if __name__ == '__main__':  
    unittest.main(warnings='ignore') 

堆栈跟踪

Creating test database for alias 'default'...
EException ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x103f652b0>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 151, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 123, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'

======================================================================
ERROR: test_can_start_a_list_and_retrieve_it_later (functional_tests.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/timothybaney/Treehouse/TDD/superlists/functional_tests.py", line 13, in setUp
    self.browser = webdriver.Firefox(capabilities=caps)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 82, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 62, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
NotADirectoryError: [Errno 20] Not a directory

----------------------------------------------------------------------
Ran 1 test in 0.012s

FAILED (errors=1)
Destroying test database for alias 'default'...

最佳答案

该错误是因为您使用的是 FF 48。对于 FF>=47,FirefoxDriver 停止工作。您必须使用新的 MarionetteDriver

设置这个:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True

browser = webdriver.Firefox(capabilities=caps)
browser.get('http://localhost:8000')

assert 'Django' in browser.title

关于python - Selenium 无法打开 Firefox 48.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39323817/

相关文章:

javascript - 为什么 getPreventDefault() 起作用而 defaultPrevented 不起作用?

python - Mac 操作系统 : How do I run a multi-line script on command line?

python - 使用 Opencv 和 python 获取 RTSP 流

python - Flask-Python 空 html 输入返回值的特征

java - 如何只有一个驱动程序可以用作Webdriver或Appiumdriver

javascript - 通过绑定(bind)获取Protractor中的UI-Grid列数据

python - python对象和类的内存布局是怎样的?

python - 抓取隐藏框架 JavaScript

firefox - 使用 Add-on SDK 添加工具栏按钮?为 Firefox 附加组件集成 XUL 和附加 SDK?

selenium - Firefox - 使用 Selenium 隐藏浏览器框架