Selenium usando Python: el ejecutable de Geckodriver debe estar en PATH

Resuelto tadm123 asked hace 7 años • 37 respuestas

Estoy repasando el texto Automatiza las cosas aburridas con Python de Sweigart . Estoy usando IDLE y ya instalé el módulo Selenium y el navegador Firefox.

Siempre que intento ejecutar la función webdriver, aparece esto:

from selenium import webdriver
browser = webdriver.Firefox()

Excepción:

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>
Traceback (most recent call last):
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
    self.stop()
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>>
Traceback (most recent call last):
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
    self.stop()
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Traceback (most recent call last):
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "C:\Python\Python35\lib\subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "C:\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

Durante el manejo de la excepción anterior, ocurrió otra excepción:

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    browser = webdriver.Firefox()
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
    self.service.start()
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

Creo que necesito establecer la ruta para geckodriver, pero no estoy seguro de cómo, entonces, ¿cómo haría esto?

tadm123 avatar Oct 24 '16 04:10 tadm123
Aceptado

selenium.common.exceptions.WebDriverException: Mensaje: el ejecutable 'geckodriver' debe estar en PATH.

En primer lugar, deberá descargar el geckodriver ejecutable más reciente desde aquí para ejecutar la última versión de Firefox usando Selenium.

En realidad, los enlaces del cliente Selenium intentan localizar el geckodriverejecutable del sistema PATH. Deberá agregar el directorio que contiene el ejecutable a la ruta del sistema.

  • En sistemas Unix, puedes hacer lo siguiente para agregarlo a la ruta de búsqueda de tu sistema, si estás usando un shell compatible con Bash:

    export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
    
  • En Windows, deberá actualizar la variable del sistema Ruta para agregar la ruta completa del directorio al geckodriver ejecutable manualmente o mediante la línea de comando ** (no olvide reiniciar su sistema después de agregar el geckodriver ejecutable a la RUTA del sistema para que surta efecto)**. El principio es el mismo que en Unix.

Ahora puede ejecutar su código de la misma manera que lo hace a continuación: -

from selenium import webdriver

browser = webdriver.Firefox()

selenium.common.exceptions.WebDriverException: Mensaje: Ubicación binaria esperada del navegador, pero no se puede encontrar el binario en la ubicación predeterminada, no se proporciona la capacidad 'moz:firefoxOptions.binary' y no se establece ningún indicador binario en la línea de comando

La excepción indica claramente que instaló Firefox en otra ubicación mientras Selenium intenta encontrar Firefox e iniciarlo desde la ubicación predeterminada, pero no pudo encontrarlo. Debe proporcionar explícitamente la ubicación binaria instalada de Firefox para iniciar Firefox como se muestra a continuación: -

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)
Saurabh Gaur avatar Oct 23 '2016 23:10 Saurabh Gaur

Esto me lo resolvió.

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')
Nesa avatar Feb 08 '2017 19:02 Nesa

Estos pasos me lo resolvieron en Ubuntu y Firefox 50.

  1. Descargar geckodriver

  2. Copiar geckodriver a la carpeta/usr/local/bin

No es necesario agregar:

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)
Andrea Perdicchia avatar Dec 02 '2016 12:12 Andrea Perdicchia