gunicorn[15246]: Message: 'geckodriver' executable needs to be in PATH - selenium

I'm currently trying to run a python script using selenium and geckodriver on an Ubuntu 20.04 server. I'm using gunicorn and nginx to setup my flask application, but I've been trying for days now to use every answer on all similar articles, but still nothing is working for me as it is for others.
Anyone have any ideas what I'm doing wrong?
Firefox version – Mozilla Firefox 81.0
Selenium version - 3.141.0
Geckodriver version – 0.27.0
Code: (I've tried each of these 6 below)
def login(self):
try:
print("Logging in...")
# 1)
ff_options = FirefoxOptions()
ff_options.headless = True
self.driver = webdriver.Firefox(options=ff_options)
# 2)
ff_options = FirefoxOptions()
ff_options.headless = True
self.driver = webdriver.Firefox(executable_path="./geckodriver", options=ff_options)
# 3)
self.driver = webdriver.Firefox()
# 4)
self.driver = webdriver.Firefox('./geckodriver')
# 5)
binary = FirefoxBinary('/usr/local/bin/geckodriver')
browser = webdriver.Firefox(firefox_binary=binary)
# 6)
binary = FirefoxBinary('/usr/bin/firefox')
driver = webdriver.Firefox(firefox_binary=binary, executable_path="/home/ubuntu/cole/instagram_bot_v2")
self.driver.get('https://www.instagram.com/accounts/login')
Error I'm receiving:
Oct 12 07:00:11 ip-172-31-32-84 gunicorn[15245]: Message: 'geckodriver' executable needs to be in PATH.
The geckodriver executable itself I've tried placing in /usr/bin, /usr/local/bin and also just in my project directory, all are set to executable, but it doesn't seem to make a difference when running it, they all get the same path error.
Anyone have any ideas what I could try?

One thing that you can do is to move your geckodriver to the same directory where your program is saved. Then, you can start the geckodriver just by using webdriver.Firefox() instead of specifying the full path of the executable file. This would save you a lot of time.

Related

Add geckodriver to PATH on Windows 11

To complete a project using Selenium, I first need to add Geckodriver to PATH. I have read many articles about adding elements to PATH, but none of them end up working (I follow the steps and add a new element to PATH, but when I run my program I get the 'geckodriver' executable needs to be in PATH error.
1: I first download this version of Geckodriver from here: https://github.com/mozilla/geckodriver/releases
2: I extract the folder and add it to the PATH
3: When I run my program, I get the same error Message: 'geckodriver' executable needs to be in PATH.
Any help would be appreciated!
You do need to add an environment variable for the firefox driver.
String exePath = ".\\lib\\geckodriver.exe"; System.setProperty("webdriver.gecko.driver", exePath); driver = new FirefoxDriver();
You don't have to add a new environment variable, just move the file geckodriver.exe to your python folder, which in my case is
C:\Users\username\AppData\Local\Programs\Python\Python310
Then to start the firefox driver just run
from selenium import webdriver
driver = webdriver.Firefox()

I can't open firefox using geckodriver.exe

I'm beginner of selenium.
I want to open firefox using geckodriver.exe.
Here is my code.
from selenium import webdriver
import os
path = os.getcwd()
firefox_driver_path = str(path)
browser = webdriver.Firefox(firefox_driver_path)
I have confirmed that geckodriver.exe is in the same directory.
Error code
Message: Service geckodriver unexpectedly exited. Status code was: 1
​
I couldn't find a clear resolution to the above error.
I'm sorry, but please tell me why this error happened and how to resolve it.
Environment: Windows11, Python 3.8.8, selenium-4.1.2
I have resolved this trouble by updating geckodriver.exe to newer version.

Install and use Geckodriver on Heroku correctly [duplicate]

from selenium import webdriver;
browser= webdriver.Firefox();
browser.get('http://www.seleniumhq.org');
When I try to run this code, it gives me an error message:
Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line.
Any thoughts-highly appreciated!
This error message...
Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line.
...implies that the GeckoDriver was unable to find the Firefox binary at the default location. Additionally you haven't passed the moz:firefoxOptions.binary capability.
Solution
Possibly within your system firefox is installed in a custom location and these cases you need to pass the absolute path of the Firefox binary through the moz:firefoxOptions.binary capability as follows:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(executable_path=r'C:\WebDrivers\geckodriver.exe', options=options)
driver.get('http://google.com/')
References
You can find a couple of relevant detailed discussion in:
SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary'
InvalidArgumentException: Message: binary is not a Firefox executable error using GeckoDriver Firefox Selenium and Python
Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided
Firefox was not installed on my system at all. That's why this error came up.
same issue here:
Environment
OS: Mac
Not install Firefox application
has installed geckodriver, can found in PATH
Error Reason: Not installed Firefox
Solution: (goto firefox official site to download and) install Firefox
Before this ensure that path variable has include for geckodriver click here to download driver and run below python script.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(options=options)
driver.get('http://google.com/')
I have uninstalled firefox and installed it again which resolved my issue.
You should download appropriate web driver from https://github.com/mozilla/geckodriver/releases and put it into folder where your py file is. Also you can put it anywhere as long as the location of the file it is in your system path.
Selenium uses a web driver (a specific one for each web browser) in order to communicate with the browser installed on your system (Firefox in your case).
To use Firefox, you have to:
Download its web driver from
https://github.com/mozilla/geckodriver/releases
Put the web driver in a specific location in the file system (same folder as the python script for example)
Add the web driver location path when initializing in the python code.
So the final code would look like this:
from selenium import webdriver
browser = webdriver.Firefox('./geckodriver')
browser.get('https://www.python.org/')
Note: Sometimes a newer version of the web driver isn't compatible with an older version of the browser installed on your system.
I have encountered the same problem (Windows, Firefox v99, Selenium 4.1.4, geckodriver 0.31.0), the path to exe file and the driver initialisation were set correctly, solved the issue by changing the win32 by win64 version of geckodriver
as a side note for selenium/firefox (but with C#, not Python), this issue is quite relevant now in the sense that firefox location looks to be stored in windows in a new regedit location. Indeed geckodriver is looking in regedit location documented here:
HKEY_LOCAL_MACHINE\SOFTWARE WOW6432Node\Mozilla\Mozilla Firefox\[VERSION]\Main\PathToExe
HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox\[VERSION]\Main\PathToExe
Source:
https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions
when on my machine it is there:
HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox 109.0\bin
With the version number stored here:
HKEY_LOCAL_MACHINE\SOFTWARE\mozilla.org\Mozilla
and I set the selenium driver with C# Api with (path hardcoded for the poc):
var options = new FirefoxOptions();
...
options.BrowserExecutableLocation = #"C:\Program Files\Mozilla Firefox\firefox.exe";
Driver = new FirefoxDriver(options);
Regards
You need to download geckodriver.
https://github.com/mozilla/geckodriver/releases
from selenium import webdriver;
browser= webdriver.Firefox('./geckodriver');
browser.get('http://www.seleniumhq.org');

selenium.common.exceptions.webdriverexception: message: 'chromedriver.exe' unexpectedly exited.status code was: 1

I'm new at Python and I'm trying to use selenium webdriver to do web scraping from a webpage. When I run my code, I have not problems in obtaining the results I need, but when someone else tries to run the code from an executable it shows the error: selenium.common.exceptions.webdriverexception: message: 'chromedriver.exe' unexpectedly exited.status code was: 1. The path where I'm saving chromedriver.exe is a public repository. Can someone help me with this please? Here is the piece of code I'm using:
from selenium import webdriver
url= "https://www.byma.com.ar/obligaciones-negociables/"
driver = webdriver.Chrome(executable_path=r'\\path\\chromedriver.exe')
driver.implicitly_wait(30)
driver.get(url)
time.sleep(2)
You do not need to setup a driver like this :
driver = webdriver.Chrome(executable_path=r'\\path\\chromedriver.exe')
do this :
This is a pre - requisite
Installation
pip install chromedriver-autoinstaller
Usage:
Just type import chromedriver_autoinstaller in the module you want to use chromedriver.
Example
from selenium import webdriver
import chromedriver_autoinstaller
chromedriver_autoinstaller.install() # Check if the current version of chromedriver exists
# and if it doesn't exist, download it automatically,
# then add chromedriver to path
driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
and the export this changes to your executables

Program using selenium fails after building with cx_freeze

I'm developing an automatic web tester using Selenium (v2.37.2). Program works properly until I run the test built with cxfreeze (there is also tkinter gui).
there is the init function
def initDriver(self):
if self.browser == FIREFOX:
profile = webdriver.FirefoxProfile(profile_directory=self.profile);
self._driver = webdriver.Firefox(firefox_profile=profile)
elif self.browser == CHROME:
self._driver = webdriver.Chrome(self.executable, chrome_options=profile)
elif self.browser == IEXPLORER:
self._driver = webdriver.Ie(self.executable)
Now when I build it using Cx_freeze I get this error
method redirectToBlank(...) calls initDriver(..) as the first thingSo how I pack the .xpi file to the library.zip file - which option in setup.py I have to use? And do I even have to this?
And the second strange thing is, that the other browsers work fine, when I execute the .exe file in by clicking on its icon, but when I run it from command line, I get errors even for chrome and IE. (Sorry that the traceback isn't complete)
All paths are relative from the executed file (no matter from where you run it),
Thank you for any ideas to solve this problem.
(method redirectToBlank(...) calls initDriver(..) as the first thing)
First issue solved
It's problem with selenium - FirefoxProfile - class, which tries to load webdriver.xpi as a normal file, but selenium pack all libraries to a zip file, so selenium can't find it.
Even forcing cx_freeze in setup file to add webdriver.xpi to a proper directory in zip won't help.
It is necessary to edit FirefoxProfile (in firefox_profile module) class for example like this
def _install_extension(self, addon, unpack=True):
"""
Installs addon from a filepath, url
or directory of addons in the profile.
- path: url, path to .xpi, or directory of addons
- unpack: whether to unpack unless specified otherwise in the install.rdf
"""
if addon == WEBDRIVER_EXT:
# altered lines
import sdi.env
WEBDRIVER_SUBSTITUTE = "path/to/unpacked/webdrive.xpi"
addon = os.path.join(os.path.dirname(__file__), WEBDRIVER_SUBSTITUTE)
# Original lines:
# addon = os.path.join(os.path.dirname(__file__), WEBDRIVER_EXT)
< the rest of the method >
Issue 2
OSError: win error 6: the handle is invalid problem wasn't caused by either cxfreeze or selenium. I run the final exe file from git bash. There's the problem. For some reason git bash doesn't open stdin for the program and that's why it fails. When I run it in standard windows command line, everything is ok or if i run it from git bash like program.exe < empty_file
what i did was remove selenium form packages list.
and put it inside includefiles, then it works.
like this :
includefiles = [(seleniumPackage,'')]
...
options = {'build_exe': {'includes':includes,
'excludes':excludes,
'optimize':2,
'packages':packages,
'include_files':includefiles,
...