Selenium在Mac上的2019中使用Python

时间:2019-02-09 22:24:48

标签: python-3.x

我不是尝试使用Python自动完成无聊的东西的程序员。我看了所有视频,以为我准备好了,但是当我尝试着走时,我陷入了零平方。

我使用的是Mac OSx Mojave版本10.14.2

成功安装了Python 3.7.2

我正在尝试遵循本章:http://automatetheboringstuff.com/chapter11/

成功安装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. 

我已经尝试了以下所有方法:Selenium using Python - Geckodriver executable needs to be in PATH,但我认为它不再起作用了。

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

我尝试同时安装最新的Firefox和Firefox v46,因为我读到某个地方在该版本之后无法正常工作。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我建议使用pip安装

private static void OnEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    ListView input = obj as ListView;
    new TestClass(input);   // <== this is the mystic line
}

然后添加此代码

pip install geckodriver-autoinstaller

答案 1 :(得分:0)

设置您的可执行文件路径

from selenium import webdriver
webdriver.Firefox(executable_path=r'path\to\geckdriver\geckodriver.exe')

executable_path – 覆盖用于Firefox 47.0.1及更高版本的geckodriver二进制文件的完整路径,默认为从系统路径中提取二进制文件。

来自documentation

编辑:在较低版本的Firefox中也可以使用

编辑2:

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

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