用于Selenium的Firefox Web驱动程序

时间:2016-10-20 22:44:22

标签: python python-3.x selenium selenium-webdriver selenium-firefoxdriver

我在Python 3的OS X上使用selenium 3.0.1和firefox 48.我无法让firefox作为webdriver工作。

from selenium import webdriver
driver = webdriver.Firefox()

给出以下错误:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-21-fd567e24185f> in <module>()
----> 1 driver = webdriver.Firefox()

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/firefox/webdriver.py in __init__(self, firefox_profile, firefox_binary, timeout, capabilities, proxy, executable_path, firefox_options, log_path)
    133         if capabilities.get("marionette"):
    134             self.service = Service(executable_path, log_path=log_path)
--> 135             self.service.start()
    136
    137             capabilities.update(firefox_options.to_capabilities())

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/common/service.py in start(self)
     62             self.process = subprocess.Popen(cmd, env=self.env,
     63                                             close_fds=platform.system() != 'Windows',
---> 64                                             stdout=self.log_file, stderr=self.log_file)
     65         except TypeError:
     66             raise

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds)
    854                                 c2pread, c2pwrite,
    855                                 errread, errwrite,
--> 856                                 restore_signals, start_new_session)
    857         except:
    858             # Cleanup if the child failed starting.

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1458                             else:
   1459                                 err_msg += ': ' + repr(orig_executable)
-> 1460                     raise child_exception_type(errno_num, err_msg)
   1461                 raise child_exception_type(err_msg)
   1462

OSError: [Errno 8] Exec format error

2 个答案:

答案 0 :(得分:0)

使用selenium 3或更高版本,你需要使用gecko驱动程序来运行firefox:

Java

System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");
WebDriver driver = new FirefoxDriver();

Python

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = 'C:\Users\test\tools\Firefox'
driver = webdriver.Firefox(capabilities=firefox_capabilities)

答案 1 :(得分:0)

  1. 确保您下载的是正确的tar.gz文件。
  2. 你去https://github.com/mozilla/geckodriver/releases后 - 寻找geckodriver的macos.tar.gz版本。
  3. 在桌面上下载并打开。
  4. 现在,在Finder中选择“开始”选项,然后选择“转到文件夹”。输入/ usr / local / bin,然后将geckodriver粘贴到bin中。