Linux, selenium can't find firefox binary from within a virtual environment - linux

I'm running Pycharm and attempting to use selenium to launch and control an instance of firefox.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = "/usr/lib/firefox/firefox"
browser = webdriver.Firefox(options=options)
This produces an error:
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable
Am I writing the path of the firefox binary correctly? From the terminal within the virtual environment, there is no firefox directory in /usr/lib - presumeably because I'm in the virtual environment? From the system terminal, I can find it no problem at /usr/lib/firefox/firefox.
How do I write a path to the firefox executable in the system environment, from within my virtual environment in pycharm? What's happening here that's preventing me from seeing / accessing the system environment?
I've tried the instructions at each of the below, the issue is not that I need to set an option to the correct path - the binary is located in my system at /usr/lib/firefox/firefox - but setting the option as in the code above does not result in success, maybe because firefox does not exist at that location within the virtual environment.
I've tried all suggestions at each of the below:
Unable to open firefox using selenium-python-geckodriver
SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary'
Selenium on PyCharm cannot find firefox binary
Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided using GeckoDriver

Related

selenium-webdriver: The geckodriver executable could not be found on the current PATH

I installed the selenium webdriver using the following command:
$ npm i selenium-webdriver
Then, I created the directory D:\WebDriver\bin (and added the files geckodriver.exe and operadriver.exe to that directory) and added it to the system's PATH variables.
The PATH to the directory that contains both drivers was successfully added, as can be seen below:
$ printenv PATH
/mnt/d/WebDriver/bin/
$ ls "/mnt/d/WebDriver/bin/"
geckodriver.exe operadriver.exe
I am also able to run the geckodriver via cmd.exe (Same applies to operadriver):
C:\Users\user>geckodriver
1621873805268 geckodriver INFO Listening on 127.0.0.1:4444
When I try to run one example file (google_search) via:
/mnt/d/proj/node_modules/selenium-webdriver/example $ node google_search.js
I receive the following stacktrace:
Error: The geckodriver executable could not be found on the current PATH. Please download the latest version from https://github.com/mozilla/geckodriver/releases/ and ensure it can be found on your PATH.
The operadriver also cannot be found when setting up an example that uses the opera driver.
Worth mentioning is that I use a Windows Subsystem for Linux (WSL) and ran the commands (node, printenv and npm) in the terminal of that subsystem. The path variable was set on the Windows system and the required drivers were only installed on the Windows system. I can access the installed files on my windows system via the WSL terminal, but selenium still cannot find the specific driver(s).
When I run the above mentioned test file (google_search.js) from selenium-webdriver on the Windows system, it does find the web driver and works as expected. I still cannot figure out the reason why the driver is not being found in the WSL.
I was able to resolve the problem using the following well written article: https://qxf2.com/blog/setup-linux-testing-environment-on-windows-using-wsl/. By creating symlinks that point to the driver and the web driver executable, the driver and the web browser was found. For now, I do think it's easier to run the tests in cmd.exe, though, as it does not require these additional steps.

How can I fix this geckodriver error in Python?

I started learning selenium, and when I ran this code...
from selenium import webdriver
browser = webdriver.Firefox()
print(type(browser))
When I ran that code, I got this error - Message: 'geckodriver' executable needs to be in PATH.
How can I fix this error? I'm working on Firefox 64bit, and my Selenium Version is '3.141.0'.
gekodriver is detected in the 3 following ways.
1. Using environment variable PATH
Add the absolute path to the gekodriver directory into the environmental variable PATH.
export PATH=PATH:/ABSOLUTE/PATH/TO/PARENT_DIRECTORY_OF/gekodriver
2. Current directory
Keep the gekodriver executable in the current directory where the Python script is being executed from.
3. Passed as executable_path
For example,
webdriver.Firefox(executable_path="c:\\gekodriver.exe")
The error is fixed, I just put the geckodriver.exe into the Python39 folder and it works now perfectly!

Selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

I am trying to automate some stuff I do day-to-day online and get some experience in webscraping. I saw some stuff about Selenium and Python being good for beginners and thought I would give it a try. I ended up getting a simple script to work on my home computer but it doesn't find the Chrome binary at the path I am giving and I do not know what to do.
I used this to set my PATH environment variable to the directory that holds my chromedriver https://youtu.be/dz59GsdvUF8
I downloaded ChromeDriver 74.0.3729.6 (latest stable release) from https://sites.google.com/a/chromium.org/chromedriver/
I am using python3, visual studio code, and Ubuntu if any of that matters.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#Stack overflow said to try this but could not make it work
#opts = webdriver.ChromeOptions()
#opts.binary_location = "C:\webdrivers\chromedriver.exe"
#browser = webdriver.Chrome(chrome_options=opts)
#this is where my chrome is
#C:\Users\tucker.steven2\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Virtual Programs\Google Chrome\Chrome v62
#I have a chromedriver in the same directory as my python code
browser = webdriver.Chrome('chromedriver.exe')
browser.get(https://www.something.com/) #just a generic site
browser.quit()
My expected result is to open a chrome window to Hulu's website, the error I get is:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 10.0.16299 x86_64)
If you are on Ubuntu, which is a distribution of Linux, then your path is all wrong. The YouTube video was for Windows. The file structure is completely different on Linux. Please update with your current operating system where you are trying to run this. Also you don't need to write a script - just to open hulu. You can just make a shortcut and put it on your desktop. If you are trying to do something more complicated that takes a long time to do, that would be a better use case for making a selenium script. I get that you are trying to learn this, which is commendable. If you are really on Ubuntu as your operating system, then you would have a /usr/bin folder where your programs are. If you are running Windows, it would be C:\Users\"Your User Name Goes Here"\Documents or where ever you were going.
Programs would be in C:\Program Files\ or C:\Program Files(x86). I would recommend watching some YouTube videos about the path on your operating system.
The answer depends on your operating system and where you have saved your programs.

launch jupyter notebook in specified browser on cmd line

I'm working on a computer that belongs to my company. I don't have admin privileges, in case that's relevant.
My default browser in Windows 10 is set to InternetExplorer. This is mandated by my company. (Edge or Chrome not allowed as default browser) Although I do have Edge and Firefox installed.
I know that I can change the default browser for jupyter notebook sessions by editing the Jupyter config file. But I don't want to change the default in Jupyter.
Instead, I would like to invoke "jupyter notebook" on the command line (Anaconda prompt) while specifying which browser I want it to pick up.
something like:
jupyter notebook --useBrowser edge
or similar. Is this possible?
Try this:
jupyter notebook --browser="C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe %s --new-window"
It is supposed to work like this too: jupyter notebook --browser="firefox" but doesn't in practice for me, because the python webbrowser library (which is used here) is only aware of the default browser. This needs to be manually registered.
You can check this on your system, by running a python prompt and checking:
import webbrowser
list(webbrowser._browsers)
Out: ['windows-default', 'c:\\program files\\internet explorer\\iexplore.exe']
Noticed that if I open Firefox, use the cli command from Kai Aeberli above, as...
jupyter notebook --browser="C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe %s --new-window"
and copy and paste one of the URLs from the cli output into the Firefox (non-default browser) address bar, Jupyter Notebook opens in Firefox!
Thanks for this solution! :))

WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. - selenium

Before you let me know that this is a duplicate, the issue I am facing is that I am running my python code using a jupyter-notebook via an Ubuntu subsystem on Windows.
I have made sure that the path to the folder on Windows where chromedriver is located is correct. However, I am still getting the same error. Further, I am unable to see where my files from the Ubuntu subsystem are (ie, I can't find a folder on Windows with those files).
Anyway in which I can correct this? such as editing the ~/.bashrc file on Ubuntu?
browser = webdriver.Chrome("C:/Users/vr235/Downloads/chromedriver/chromedriver.exe")
Error:
WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
EDIT:
While a solution was pointed by user DebanjanB and marked as duplicate, I tried the user's suggestion but got the same error.
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Users\vr235\Downloads\chromedriver\chromedriver.exe')
browser.get("https://www.sciencedirect.com/")
Error:
WebDriverException: Message: 'C:\Users\vr235\Downloads\chromedriver\chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
In case one is using a WSL, one can identify the path where the Chromedriver is present by viewing the mnt folder on WSL.
For instance: My chromedriver path was in the Downloads folder in C:/ drive
Path : /mnt/c/vr235/Users/Downloads/chromedriver.exe
Once you add the same, the above lines of code work perfectly.

Resources