I wrote a small automation script using Python and Selenium Web Driver and I made it into an executable and the executable worked fine on my computer but when I tried it on another computer I got that chromedriver isn't in path (note: I included chromedriver using the add binary command and both computers are running the same operating system: Mac OS)
I am not quite sure why this happened, my suspicion is the following:
In my .py code I used the following two lines to initiate the chromedriver
path = "/Users/sergio/chromedriver"
driver = webdriver.Chrome(path)
However, the other computer doesn't even have this path so might be causing it even has this path. Now one might suggest simply using driver = webdriver.Chrome() but here comes the bigger problem: I tried having my chromedriver in a system path like /usr/local/bin and when I did that driver = webdriver.Chrome() worked but compiling the program using pyinstaller didn't work, I ran:
pyinstaller --onefile --noconsole --add-binary 'usr/local/bin/chromedriver:.' finalregtool.py
but I got a lot of errors including Unable to find "/Users/sergio/usr/local/bin/chromedriver" when adding binary and data files
This led to me thinking that pyinstaller automatically adds /Users/sergio to the given path which makes my path wrong so I had to use something inside /Users/sergio , I tried putting my chromedriver in Applications but compilation resulted in not finding the path again, for some reasons compilation worked when the path was /Users/sergio/chromedriver so I went with this path but the code didn't work anymore (chromedriver not found in path) so I had to specify the path manually.
Now I am kind of stuck in a deadlock not knowing what to do. Any help would be appreciated. I hope my explanation was clear enough.
My only end goal is compiling my selenium script and running it on other computers without having them install anything extra, I don't care how this should be done so if anyone has a solution outside pyinstaller I don't mind.
Update: added a bounty
There're couple of good solutions for that issue:
1.Install webdriver-manager package:
pip install webdriver-manager
what I prefer mostly and use it like:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
2.Launch Remote Selenium Hub called Selenoid and lauch browser remotely:
driver = webdriver.Remote(command_executor='ip_of_your_selenoid_instance:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)
3.You can launch Chrome docker container and also going to by remote url, what will be always localhost:
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)
You need to launch a standalone chrome browser:
docker run -d -p 4444:4444 selenium/standalone-chrome
and then in your python script launch browser using Remote webdriver:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", DesiredCapabilities.CHROME)
docker-compose:
# docker-compose.yml
selenium:
image: selenium/standalone-firefox
ports:
- 4444:4444
pyinstaller unpacks everything into a temporary directory during execution of exe, when using the --onefile mode. Refer to this answer and the related post to get an idea about how to get the correct relative path. You might have to use
path = resource_path("chromedriver")
Also, you can directly add the chromedriver to PATH environment variable in the runtime so you wouldn't have to specify the path explicitly and you can simply call webdriver.Chrome()
os.environ["PATH"] += os.pathsep + resource_path("chromedriver")
pyinstaller conciders usr/local/bin/chromedriver:. as a relative path, thus adds /Users/sergio in an attempt to make it absolute, try using a different location. This location doesn't matter anyway, once this file is found pyinstaller will add this to the root directory of the compiled exe, and unpack it when the exe is executed.
The modern way to deal with the chromedriver binary is to use WebDriverManager.
First you need to install the manager:
pip install webdriver-manager
Then you use it (for Chrome):
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
In my .NET Core project I use not the most modern approach I have described, but the latest stable NuGet from here https://www.nuget.org/packages/Selenium.WebDriver.ChromeDriver/.
Basically the nuget contains the chromedriver and copies it every time to your assembly folder (\bin\Debug\netcoreapp3.1 in my case). This is the line I have in my project:
"<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="*" />"
The star means the latest (highest number) stable package will be used as explained here. Of course you can install the latest version manually, and then on each 2 major Chrome versions to update the NuGet package reference so that is matches your Chrome version. (87 chromedriver works with 87 and 88 Chrome, but not with 89).
Then the driver is instantiated with this lines:
var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
References:
Error message: "'chromedriver' executable needs to be available in the path"
https://www.automatetheplanet.com/webdriver-net50/
path = "/Users/sergio/chromedriver"
driver = webdriver.Chrome(path)
Change the above code to the following format
driver = webdriver.Chrome(executable_path='C:/Users/sergio/chromedriver.exe')
I'm up with python 3.8 and selenium but recently I downloaded the latest edge web driver zip file and runned the mswdedriver.exe from that and typed this code in my ide:
from selenium import webdriver
browser = webdriver.Edge('F:\za\python\Assistant\msedgedriver.exe')
browser.maximize_window()
browser.get(url='http://seleniumhq.org/')
but I see this error:
selenium.common.exceptions.WebDriverException: Message: 'MicrosoftEdgeDriver' executable needs to be
in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687
Can you help me friends?
Thanks in advance.
You need to give a path to the webdriver executable when you load a webdriver, or have it stored as an environment variable:
webdriver.Edge(executable_path="path/to/executable")
A web driver is essentially a special browser application, you must install that application before you can run anything with it.
Here's Edge's web driver download page. Or you can use the link from the error message http://go.microsoft.com/fwlink/?LinkId=619687
Here's a similar question Python Selenium Chrome Webdriver
The backslashes in the executable path need to be escaped, per Python syntax:
browser = webdriver.Edge('F:\\za\\python\\Assistant\\msedgedriver.exe')
This problem appears to me
you must put in "Bin Folder" the file "MicrosoftWebDriver.exe" as is , edit the previous name of edge webdriver to be "MicrosoftWebDriver.exe" and put it in the "Bin Folder"
You need to download the browser driver from here
After the download completes, extract the driver executable to your preferred location. Add the folder where the executable is located to your PATH environment variable.
I've looked around checked both documentations and have found no answer.
I've been trying to use InstaPy a instagram api for python. After failing with multiple errors and assuming InstaPy is just having some issues so i tried to raw code it using selinium. after inserting the example code and alter it to my liking i just made sure this one would work. I received a new error instead of the old one saying the permissions may not be right. I have tried reinstall and running as admin but nothing works. how do i fix this and/or what does this mean
Code:
import time
from selenium import webdriver
driver = webdriver.Chrome('C:\Webdrivers') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()
Error:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Webdrivers\RawBot.py", line 5, in <module>
driver = webdriver.Chrome('C:\Webdrivers') # Optional argument, if not specified will search path.
File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
self.service.start()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 86, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'Webdrivers' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
This error message...
WebDriverException: Message: 'Webdrivers' executable may have wrong permissions.
...implies that the ChromeDriver variant you are trying to use have wrong permissions.
You seem to have tried out:
driver = webdriver.Chrome('C:\Webdrivers') # Optional argument, if not specified will search system $PATH variable.
A few words:
If your underlying os is windows:
You have to download chromedriver_win32.zip from the ChromeDriver Download Location and unzip it for usage.
Additionally, if you are explicitly specifying the Chromedriver binary path you have to append the binary extension as well, effectively i.e. chromedriver.exe.
While mentioning the Chromedriver binary path you have to either use the single forward slash i.e. (/) along with the raw (r) switch or you have to use the escaped backslash i.e. (\\).
So your effective line of code will be :
driver = webdriver.Chrome(executable_path=r'C:/path/to/chromedriver.exe')
If your underlying os is linux:
You have to download chromedriver_linux64 from the ChromeDriver Download Location and untar it for usage.
Additionally, if you are explicitly specifying the Chromedriver binary path you don't have to provide any extension for the executable binary, effectively i.e. chromedriver.
While mentioning the Chromedriver binary path you have to use the single forward slash i.e. (/).
So your effective line of code will be :
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
If your underlying os is macos:
You have to download chromedriver_mac64 from the ChromeDriver Download Location and untar it for usage.
Additionally, if you are explicitly specifying the Chromedriver binary path you don't have to provide any extension for the executable binary, effectively i.e. chromedriver.
While mentioning the chromedriver binary path you have to use the single forward slash i.e. (/).
So your effective line of code will be :
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
This got solved when you enter the full file name which is "chromedriver.exe". Try this if you are on windows
You just have to add
/chromedriver.exe
at the end of the path like this:
driver = webdriver.Chrome('C:/Users/User/Downloads/chromedriver_win32/chromedriver.exe')
Note: If you copy the path from "File Explorer" you will get:
C:\Users\User\Downloads\chromedriver_win32
You will need to use double backslashes like this:
C:\\Users\\User\\Downloads\\chromedriver_win32
so you don't get a syntax error. Or you can just use forward slashes.
If you are on a linux os, changing file permissions could possibly fix the problem. But beware of what you do with permissions:
chmod 755 "/path to chromedriver file"
I downloaded the file via python itself, which unfortunately disabled execution permission and this was the quick fix for it.
in Linux you need to specify the FULL path including the file itself as well, not just up to the folder (in Linux by default the downloaded file for chrome driver comes in a folder, e.g. 'chromedriver_linux64', open the folder and check the exact file name as well, usually 'chromedriver'):
from selenium import webdriver
chrome_driver_path = "/home/mn/chromedriver_linux64/chromedriver"
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get("https://www.amazon.com")
if you are using chrome you must specify the full path of the chromedriver.
search for the directory in which your chromedriver executable file resides.
click shift+right click on the executable file.
select "copy as path" and paste it in your script.
don't forget to use a double backslash
so it should be:
driver = webdriver.Chrome('C:\\Utility\\BrowserDrivers\\chromedriver.exe')
If you use a Mac OS Big Sur, and Chromedriver 90+, you might have an error with the permissions of your Chromedriver.
If this is the case, it's not the file permissions that you change via "chmod," instead, it is apple-store protection that forbids the execution of the web driver.
If you like to allow this driver to be used within your Python environment or via command-line, enter:
`$ sudo xattr -r -d com.apple.quarantine /usr/local/bin/chromedriver`
had same issue in django.
However when I ran the same code locally (not activating my django app) it was fine and didn't have to explicitly define the path to the chrome driver.
Got around it by explicitly defining the path and the chromederiver.exe
similar to the answer above.
path = "C:/Users/YOUR_USER/Desktop/chromedriver/chromedriver.exe"
in my case, since I want to eventually post my app I used dynamic paths
ie.
import os
BASE_oaAPP_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_oaAPP_Utilities_DIR = os.path.join(BASE_oaAPP_DIR, 'mySubFolderInTheApp')
def utilsPathFileName(fileName):
return os.path.join(BASE_oaAPP_Utilities_DIR, fileName)
chrome_path = utilsPathFileName('chromedriver.exe')
driver = webdriver.Chrome(chrome_path)
WINDOWS : Giving complete file path solved problem.
Many tutorials and articles are recorded with previous versions, where you just give directory path containing web drivers. But now you also have to provide specific driver path you're using.
Instead of C:/WebDrivers give complete file path as C:/WebDrivers/chromedriver.exe
chmod 755 "/path to chromedriver file"
This has helped me
The same problem. My solution was in Linux os, Pycharm editor...
Make sure your webdriver file with other python files (such as selinum_driver.py)
Then just copy the path and paste it into your webdriver.Chrome('path')
Don't need to write webdriver.exe
driver = webdriver.Chrome('/home/raju/Documents/Python-selenium/chromedriver')
For WINDOWS 10: I ran into a similar situation with the same error message when running my test code in Visual Studio Code. What solved it for me was to close out of the VS Code app and re-open the app as an administrator via the right-click menu.
For me, none of the answers above worked. But moving the chromedriver.exe to a new path (desktop in my case) solved it.
path = "C:/Users/YOUR_USER/Desktop/chromedriver/chromedriver.exe"
I got the same error when wrongly installed drives(when for mac was downloaded windows drivers) once I correct it worked fine
You need to add exe at the end of the path of driver and it works.
locate your installed driver.exe,
shift+right click,
copy as path,
paste it to your IDE
we can fix this issue for centos
#Install package chromedriver. Install it using yum
yum install chromedriver
#Import following package in python.
from selenium import webdriver
#Add following options before initializing the webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
chromeOptions.add_argument("--remote-debugging-port=9222")
chromeOptions.add_argument('--no-sandbox')
driver = webdriver.Chrome('/usr/bin/chromedriver',chrome_options=chromeOptions)
If you run it on a Windows computer you can also add it to Windows PATH (environment variables) so you don't have to declare executable_path. You can just say: webdriver.Chrome(options=your_options)
I got his error for MacOs and on changing the path to the one given below, the issue was solved.
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
I kept having this SAME problem after:
my script did not recognize chromdriver.exe in my $PATH (yes, it's there)
gave run permissions to exe
passed 'path/to/chromedriver.exe' into Chrome()
This is what solved it:
Don't give .exe in the python path(just provide up to the chromedriver in the path)
example:
driver = webdriver.Chrome("/Library/Frameworks/Python.framework/.../webdriver/chrome/chromedriver")
Unfortunately I still cant' get my script to read the chromedriver.exe path from my $PATH variable. Would still appreciate if someone helped out, although it's beyond the scope of this question
Windows users and visual studio code: while mentioning the Chromedriver binary path you have to either use the single forward slash i.e. (/) along with the raw (r) switch or you have to use the escaped backslash i.e. (\).
as well use the exact path and add chromedriver.exe at the end of the path to point directly to the executable:
driver = webdriver.Chrome(executable_path=r'C:/path/to/chromedriver.exe')
or:
driver = webdriver.Chrome(r'C:\\Users\\wanja\\Downloads\\Compressed\\chromedriver_win32\\chromedriver.exe')
this error means that file chromedriver is not executable,
I fix this error with run command of (in linux):
sudo chmod +777 chromedriver
It means to make the file executable by everyone with access.
I'm just trying to do something very basic on my Mac using selenium and I can't even open a webpage. I'm getting an error of :
Traceback (most recent call last):
File "/Users/godsinred/Desktop/InstagramLiker/GmailAccountGenerator.py", line 10, in <module>
driver = webdriver.Chrome()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 88, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Here is my code below:
from selenium import webdriver
import time
link = "https://accounts.google.com"
driver = webdriver.Chrome()
driver.get(link)
time.sleep(5)
driver.quit()
Most answers here and in other related posts suggest users to just move the file to /usr/bin and they work fine if you are just running chromedriver locally and normally.
However, if you are compiling Python scripts into executables using compilers such as cx_freeze, you may not be able to afford the luxury if your program always uses a relative link to chromedriver.
As the error message suggests, your compiled program does not have the permissions to manipulate chromedriver. To use a relative link to chromedriver on a Mac in your compiled Python program, you can programmatically change the permission of chromedriver in your Python script using:
import os
os.chmod('/path/to/chromedriver', 0755) # e.g. os.chmod('/Users/user/Documents/my_project/chromedriver', 0755)
You can test this by doing the following:
cd to your working directory
$ chmod 755 chromedriver to allow your program to manipulate it
P.S. 755 is the default numerical permission for files in usr/bin. 664 is the default numerical permission for files in other normal folders (probably your working directory). Thus, when chromedriver complains it does not have the correct permission, you need to grant it a numerical permission equivalent to or greater than 755.
The error says it all :
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
The error clearly mentions that the chromedriver which is getting detected have wrong permissions.
Solution
Download the latest chromedriver binary from ChromeDriver - WebDriver for Chrome and save it in your system.
Ensure that chromedriver binary have the required permissions.
While initiating the WebDriver and WebClient pass the argument executable_path along with the absolute path of the chromedriver binary as follows :
from selenium import webdriver
link = "https://accounts.google.com"
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
driver.get(link)
Reference
You can find a detailed relevant discussion in:
'Webdrivers' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
If you are on windows give path including file name. For example,
'./chromedriver/chromedriver.exe'
My line of code looks like below.
service = webdriver.chrome.service.Service('./chromedriver/chromedriver.exe')
This worked! I followed these instructions to update PATH: https://www.kenst.com/2015/03/installing-chromedriver-on-mac-osx/
I dragged my chromedriver.exe from Finder into Terminal (/etc/paths), and then copied the address in Terminal and dropped it into my Python IDE where the PATH should be inserted.
Check out a this topics
1- If you are using Linux, access the folder containing the file
Chromedriver.exe set on 755
2- check correct path for Chromedriver.exe file in your code
3- If you are using Windows servers,check the Chromedriver.exe file is available for the current user (not only the admin does have access to Chromedriver.exe - see in c://users...)
What worked for me in Windows was adding the location of the driver to Windows local PATH var, restarting my python environment so that the path to the driver displayed after running this:
import os;path = os.getenv('PATH'); print(path);
and then I did not specify the path when loading the driver:
from selenium import webdriver
driver = webdriver.Chrome()
if I tried putting the path in the Chrome() call it caused the permissions error. Adding it to the local environment path is enough.