Python 3.x 2019年在mac上使用Python的Selenium

Python 3.x 2019年在mac上使用Python的Selenium,python-3.x,Python 3.x,我是一个非程序员,试图用Python自动化枯燥的东西来实现这一点。我看了所有的视频,认为我已经准备好了,但当我试图开始的时候,我被困在了零度 我使用的是Mac OSx Mojave版本10.14.2 已成功安装Python 3.7.2 我试着遵循这一章: 已成功安装Selenium,因为: import selenium 没有给出错误 但当我运行这个: from selenium import webdriver browser = webdriver.Firefox() 我得到这个错误:

我是一个非程序员,试图用Python自动化枯燥的东西来实现这一点。我看了所有的视频,认为我已经准备好了,但当我试图开始的时候,我被困在了零度

我使用的是Mac OSx Mojave版本10.14.2

已成功安装Python 3.7.2

我试着遵循这一章:

已成功安装Selenium,因为:

import selenium
没有给出错误

但当我运行这个:

from selenium import webdriver
browser = webdriver.Firefox()
我得到这个错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1522, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'your\\path\\geckodriver.exe': 'your\\path\\geckodriver.exe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'your\path\geckodriver.exe' executable needs to be in PATH. 

>>> driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1522, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'your\\path\\geckodriver.exe': 'your\\path\\geckodriver.exe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'your\path\geckodriver.exe' executable needs to be in PATH. 
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/common/service.py”,第76行,开头
标准偏差=管道)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py”,第775行,在__
恢复信号,启动新会话)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py”,第1522行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“your\\path\\geckodriver.exe”:“your\\path\\geckodriver.exe”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
driver=webdriver.Firefox(可执行文件\u path=r'your\path\geckodriver.exe')
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/firefox/webdriver.py”,第164行,在__
self.service.start()
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/common/service.py”,第83行,开始
os.path.basename(self.path)、self.start\u错误消息)
selenium.common.exceptions.WebDriverException:消息:“your\path\geckodriver.exe”可执行文件需要位于path中。
>>>driver=webdriver.Firefox(可执行文件\u path=r'your\path\geckodriver.exe')
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/common/service.py”,第76行,开头
标准偏差=管道)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py”,第775行,在__
恢复信号,启动新会话)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py”,第1522行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“your\\path\\geckodriver.exe”:“your\\path\\geckodriver.exe”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
driver=webdriver.Firefox(可执行文件\u path=r'your\path\geckodriver.exe')
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/firefox/webdriver.py”,第164行,在__
self.service.start()
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/selenium/webdriver/common/service.py”,第83行,开始
os.path.basename(self.path)、self.start\u错误消息)
selenium.common.exceptions.WebDriverException:消息:“your\path\geckodriver.exe”可执行文件需要位于path中。
我已经尝试了这方面的一切:我认为它不再有效了

我安装了geckodriver并尝试将其附加到路径中,但似乎没有任何效果。geckodriver安装在/usr/local/bin/中,我通过运行export path=$path:/path/to/geckodriver和export path=$path:/usr/local/bin/geckodriver将其附加到path中,但我一直收到相同的错误

我已经尝试安装了最新的Firefox和Firefox v46,因为我在某个地方看到它在该版本之后无法工作


非常感谢您的帮助。

设置您的可执行路径

from selenium import webdriver
webdriver.Firefox(executable_path=r'path\to\geckdriver\geckodriver.exe')
可执行路径– 覆盖Firefox 47.0.1及更高版本使用的geckodriver二进制文件的完整路径,默认情况下从系统路径拾取二进制文件

编辑:也适用于较低版本的firefox

编辑2:

也许您可以验证python本身是否可以找到可执行文件

import os.path
os.path.exists(file_path)

我建议用pip安装

pip install geckodriver-autoinstaller
然后添加此代码

import geckodriver_autoinstaller

# Check if the current version of geckodriver exists
# and if it doesn't exist, download it automatically,
# then add geckodriver to path
geckodriver_autoinstaller.install()

我完全按照你发布的那样运行它,实际上没有改变路径或任何东西,我得到了同样的错误。哦,我确实在你的回溯中看到了。您的示例代码与回溯不匹配。你确定你的可执行文件在你的文件夹中吗?编辑:更新答案。