I did not know how to create an executable python program before I asked here. Thankfully I received a fast answer and was able to convert my script to an executable program. The executable works perfect but only on my computer.
These are the two error's I am receiving, I feel like I need to modify the script in order to locate the chrome driver I am not sure where Pyinstaller saved everything.
Exception in Tkinter callback
Traceback (most recent call last):
File "site-packages\selenium\webdriver\common\service.py", line 76, in start
File "subprocess.py", line 775, in __init__
File "subprocess.py", line 1178, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tkinter\__init__.py", line 1705, in __call__
File "MarijuanaDoctors.py", line 25, in search
File "site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
File "site-packages\selenium\webdriver\common\service.py", line 83, in start
selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
executable needs to be in PATH. Please see
https://sites.google.com/a/chromium.org/chromedriver/home
You can bundle your "chromedriver.exe" along with your script using Pyinstaller like this:
pyinstaller --add-binary="localpathtochromedriver;." myscript.py
This will copy the "chromedriver.exe" file in the same folder as your main .exe(Or in case of single file option of pyinstaller, this fill will be extracted in temp folder while using exe program).
In your script you can check if you are running the script normally or from bundled(exe file) mode, and choose path to chromedriver.exe accordingly.(This change in script can be common for Single file/folder bundle option of pyinstaller)
import sys
if getattr(sys, 'frozen', False ):
#Running from exe, so the path to exe is saved in sys._MEIPASS
chrome_driver = os.path.join(sys._MEIPASS, "chromedriver.exe")
else:
chrome_driver = 'localpathtochromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)
You can read about this in docs here.
Limitation:
The user of your .exe should have Chrome installed on their system and Chrome version should work with the chromedriver which is bundled.
Related
This question already has answers here:
Chromedriver executable path not found in Docker Container
(2 answers)
Closed 1 year ago.
I have a following problem.
In my scrape.py I set WebDriver using a function nastav_driver():
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.webdriver import WebDriver
import utils
def nastav_driver() -> WebDriver:
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(utils.PATH_CHROMIUM, options=options)
return driver
where utils.PATH_CHROMIUM is /usr/lib/chromium-browser/chromedriver. When I run the script, everything is OK. But When I run a docker image using sudo docker run python:0.0.1 I got this error:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/usr/local/lib/python3.10/subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/lib/python3.10/subprocess.py", line 1842, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/chromium-browser/chromedriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/scrape_prehled.py", line 242, in <module>
main()
File "/app/scrape_prehled.py", line 201, in main
driver = nastav_driver()
File "/app/scrape_prehled.py", line 36, in nastav_driver
driver = webdriver.Chrome(utils.PATH_CHROMIUM, options=options)
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
What is the problem here, please?
You should define and put the web driver exe file somewhere inside your selenium project so as when you run your project in a docker or on some other machine, for example on Windows PC, it will still work correctly.
For example my FireFox driver is located here:
./src/main/resources/geckodriver.exe
So the code line is
System.setProperty("webdriver.gecko.driver", "./src/main/resources/geckodriver.exe");
The same for Chrome and Edge drivers.
You can set the specific location of the driver exe files in your project accordingly to your project actual structure.
The syntax above is in Java, but with Python it will be quite similar.
I want to execute my python script by a batch file. I have no problem if i execute my python script manually, but if i use a batch file to execute it then i get the "'geckodriver' executable needs to be in Path" error. In both ways I use the same interpreter.
My batch file:
C:\Users\murac\PycharmProjects\IP_Klima_Strom\venv\Scripts\python.exe C:\Users\murac\PycharmProjects\IP_Klima_Strom\main.py
pause
my error warning:
Traceback (most recent call last):
File "C:\Users\murac\PycharmProjects\IP_Klima_Strom\main.py", line 166, in <module>
driver = webdriver.Firefox(firefox_profile=browser_profile, options=opts)
File "C:\Users\murac\PycharmProjects\IP_Klima_Strom\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
self.service.start()
File "C:\Users\murac\PycharmProjects\IP_Klima_Strom\venv\lib\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: 'geckodriver' executable needs to be in PATH.
My path Confi
I alreay updated my geckodriver and updated the path too, but nothing has changed. DO you have any idea to fix this?
i guess if you put you geckodriver in the same folder with script its gonna work
it seems subprocess.call function just can be used for the files with '.exe' extension.
This is the code i tried for Firefox.lnk in which this is the same code i tried for a git program that has '.exe' extension and worked without error.
import subprocess
subprocess.call('C:/users/m.m/Desktop/Programs/Firefox')
This is the error I get with Firefox.lnk :
Traceback (most recent call last):
File "C:/Users/m.m/PycharmProjects/untitled5/pros.py", line 2, in <module>
subprocess.call('C:/users/m.m/Desktop/Programs/Firefox.lnk')
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 323, in call
with Popen(*popenargs, **kwargs) as p:
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 775, in __init__
restore_signals, start_new_session)
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 1178, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
Firefox (Without extension) gives me FileNotFoundError: [WinError 2] The system cannot find the file specified.
when i try the code without extension for those programs with '.exe' extension i have no problem but with any program without '.exe' extension i get error... just like firefox that has '.lnk' extension.
To process .lnk files on Windows there is os.startfile() (Windows only) in the standard library.
If you want to add parameters to the command, you can also use the start command. It is a builtin command (no start.exe) therefore a shell is needed to run it.
You can also use shell=True to make this work!
lnk_path = 'C:/users/m.m/Desktop/Programs/Firefox.lnk'
subprocess.call(lnk_path, shell=True)
I am following this tutorial on web scraping https://www.linkedin.com/pulse/how-easy-scraping-data-from-linkedin-profiles-david-craven/. The python script is generating errors and I've already tried adding the directory to the PATH and it shows when I echo the path to the screen, but now it shows "/Users/owner/Users/owner" when there should just be one "Users/owner" in the path.
I'm using bash inside mac os High Sierra and am a data science major so DevOps​ is a challenge for me as well as learning how to post code to StackOverflow but I'm trying to document my steps so it will be easier to troubleshoot this.
I pip installed selenium
I downloaded chromedriver to the directory for my webscraping script file and double clicked it to run
I thought I added the directory to my PATH with 'export PATH=$PATH:~opt/bin:~/Users/owner/sbox/test/pandas_sqlite_dbase/chromedriver' which are the directions I found from http://osxdaily.com/2014/08/14/add-new-path-to-path-command-line/
I updated PIP
The directory I want to run the script from is '/Users/owner/sbox/test/pandas_sqlite_dbase'
There was another SO post Can a website detect when you are using selenium with chromedriver? that talked about how chromedriver with selenium was now auto detected and disabled... so am I trying to scrape with an outdated code base?
I can post my whole PATH or give other info.
from selenium import webdriver
driver = webdriver.Chrome('~/Users/owner/sbox/test/pandas_sqlite_dbase/googlechrome')
driver.get('https://www.linkedin.com')
Now I am getting a traceback error
Traceback (most recent call last):
File "/Users/owner/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/Users/owner/anaconda3/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/Users/owner/anaconda3/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: '~/Users/owner/sbox/test/pandas_sqlite_dbase/googlechrome': '~/Users/owner/sbox/test/pandas_sqlite_dbase/googlechrome'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/owner/sbox/test/pandas_sqlite_dbase/scraping_tutorial.py", line 7, in <module>
driver = webdriver.Chrome('~/Users/owner/sbox/test/pandas_sqlite_dbase/googlechrome')
File "/Users/owner/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Users/owner/anaconda3/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: 'googlechrome' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
[Finished in 0.7s with exit code 1]
[shell_cmd: python -u "/Users/owner/sbox/test/pandas_sqlite_dbase/scraping_tutorial.py"]
[dir: /Users/owner/sbox/test/pandas_sqlite_dbase]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
I would check what ~ actually is (seems you have the concept bad) usually is home dir, so, for a user, your "Users/owner", that's why you are obtaining "Users/owner/Users/owner".
To check this, you can
$>cd ~
$>pwd
Saw a lot of people have had problems like this, but in all my searches I saw a lot of conflicting and confusing information that I didn't understand - this is all a bit out of my newbie depth.
I installed Selenium in PyCharm and was attempting to run this code from the book Automate The Boring Stuff with Python:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://inventwithpython.com')
linkElem = browser.find_element_by_link_text('Read It Online')
type(linkElem)
linkElem.click() # follows the "Read It Online" link
Running it throws the following exceptions:
Traceback (most recent call last):
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/LB/Desktop/PythonProjects/AutomateTheBoringStuffProjects/generalTestingFile.py", line 2, in <module>
browser = webdriver.Firefox()
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
self.service.start()
File "C:\Users\LB\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
I've downloaded the latest geckodriver.exe (specifically the 64bit version since I'm on 64bit OS but I'm running 32bit Firefox if that's okay?), but I'm not sure where to put it.
I've looked up how to change a PATH but I don't know what exactly I'm supposed to change or where it's supposed to point to. (Firefox folders? Python folders?)
Followed someone's advice to put geckodriver.exe in C:\Users\LB\ and edit the System Path to ADD that location to the Variable called Path. And now the code works! (You can apparently put geckodriver.exe anywhere, as long as you point the path to that specific folder.)
Source: https://www.howtogeek.com/118594/how-to-edit-your-system-path-for-easy-command-line-access/
This answer was posted as a comment to the question How to set up Python 3 Selenium/Geckodriver for Firefox on Windows 10? by the OP LBoot.