'chromedriver' executable needs to be in PATH using Docker [duplicate] - python

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.

Related

FileNotFound : [WinError 2] The system cannot find the file specified” Error while using selenium [duplicate]

This question already has an answer here:
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with Headless Chrome
(1 answer)
Closed 2 years ago.
I am on Windows pro 10 and using Firefox to execute the python selenium program.
Trying to run:
from selenium import webdriver
wb = webdriver.Firefox()
wb.get("https://stackoverflow.com")
Getting the following error(please ignore the web browser in the error message) :
Traceback (most recent call last):
File "C:\Users\dilri\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\dilri\AppData\Local\Programs\Python\Python36- 32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\dilri\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 "H:\temp.py", line 2, in <module>
driver = webdriver.Chrome()
File "C:\Users\dilri\AppData\Local\Programs\Python\Python36-32\lib\site- packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
self.service.start()
File "C:\Users\dilri\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: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
As clearly mentioned in the error message, very first install the appropriate browser driver and give its path to the system variable path.
Like in my case:
from selenium import webdriver
driver_path = r"G:\Python\geckodriver.exe" #since on windows use raw strings
wb = webdriver.Firefox(executable_path=driver_path)
# to check if it's working
wb.get("https://stackoverflow.com")
Remember: the driver path must be a raw string since we are working on windows.
Also, see to it that you don't give the file name as selenium.py

FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec' error using Selenium on MAC OSX

I am trying to learn how to use selenium and python as well i am trying to follow this video :
https://www.youtube.com/watch?v=Xjv1sY630Uc&ab_channel=TechWithTim
This is the code I have :
from selenium import webdriver
PATH = "/Users/fuadhafiz/Documents\chromedriver.exec"
driver = webdriver.Chrome(PATH)
driver.get("https://stackoverflow.com")
But this is what keeps coming up on the terminal ( I am using VS Code and am on mac)
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py"
fuadhafiz#Fuads-iMac Web Scraping (1) % /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py"
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 947, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1819, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py", line 4, in <module>
driver = webdriver.Chrome(PATH)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'Documents\chromedriver.exec' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
and in the "problems" section
Anomalous backslash in string: '\c'. String constant might be missing an r prefix.
This is were the chrome driver is saved :
You were almost there. In macos systems the extension for the ChromeDriver binary isn't required. So effectively your code block will be:
from selenium import webdriver
PATH = "/Users/fuadhafiz/Documents/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://stackoverflow.com")
References
You can find a couple of detailed relevant discussions in:
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver' with GeckoDriver and Python in MAC OS
see error message you are using wrong file extension .exec instead of .exe
FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec'
try this instead
from selenium import webdriver
PATH = "/Users/fuadhafiz/Documents\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://stackoverflow.com")
I am also using mac so install chromedriver in default PATH saves so much trouble of adding path each time
brew cask install chromedriver
now you can simply call
driver = webdriver.Chrome()

Python Selenium geckodriver required in path

Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/home/ubuntu/bin/python/DLX/geckodriver': '/home/ubuntu/bin/python/DLX/geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 25, in <module>
driver = webdriver.Firefox(executable_path=geckoPath,options=options)
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/home/ubuntu/.local/lib/python3.6/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.
The above is the error I am getting while trying to use geckodriver and selenium. Geckodriver is in the path /home/ubuntu/bin/python/DLX/, that path has been added to the PATH var and I can see it when using echo $PATH. Below is the code I'm trying to get running.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver import DesiredCapabilities
from sys import platform
# setting browser to be heaadless
options = Options()
options.add_argument("--headless")
#Telling it where to find the required file
currentDir = os.path.dirname(os.path.realpath(__file__))
geckoPath = os.path.realpath(os.path.join(currentDir,'geckodriver'))
print(geckoPath)
binary = FirefoxBinary(geckoPath)
driver = webdriver.Firefox(executable_path=geckoPath,options=options)
I've looked at other posts, tried their fixes and I've still got the issue. Running it on RPI3b+ Ubuntu server if that helps.
Selenium Ver=3.141.0
geckodriver ver=v0.23.0
Edit:
PATH is as follows
/home/ubuntu/.cargo/bin:/home/ubuntu/.local/bin:/home/ubuntu/bin:/home/ubuntu/.cargo/bin:/home/ubuntu/.local/bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/bin/python/DLX
It would be fine if you create once driver folder in you project and place geckodriver there and provide path as mentioned below.
Lets say folder name is drivers
firefox_driver = "/drivers/geckodriver"
driver = webdriver.Firefox(executable_path=firefox_driver,options=options)

Python Autologin impossible to compile [duplicate]

This question already has answers here:
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with Headless Chrome
(1 answer)
WebDriverException: Message: 'chromedriver' executable needs to be in PATH while setting UserAgent through Selenium Chromedriver python
(1 answer)
Closed 3 years ago.
I wanted to try autologin with Python but I get this error when I´m compiling:
Traceback (most recent call last):
File "C:\Users\thega\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\thega\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 769, in __init__
restore_signals, start_new_session)
File "C:\Users\thega\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1172, 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/thega/PycharmProjects/untitled/test.py", line 10, in <module>
driver = webdriver.Chrome('C:/Users/thega/Downloads/chromedriverwin64/')
File "C:\Users\thega\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\thega\PycharmProjects\untitled\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: '' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Process finished with exit code 1
I´m using this code from Internet(it should work fine):
from selenium import webdriver
import time
username = 'name'
password = 'passwd'
url = 'https://www.page.net/'
driver = webdriver.Chrome('C:/Users/thega/Downloads/chromedriverwin64/')
driver.get(url)
driver.find_element_by_id('inputUsername').send_keys(username)
driver.find_element_by_id('inputPassword').send_keys(password)
time.sleep(2)
driver.find_element_by_id('loginButton').click()
PS: I´m new to Python.
PS2: I´m using PyCharm if that helps.
And yes, I have already installed selenium.
Thanks for any help.

selenium script doesn't run in python

Tried executing
from selenium import webdriver
browser=webdriver.Chrome()
browser.get('http://www.google.com')
but doesn't execute and throws error
=RESTART: C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\drive.py =
Traceback (most recent call last):
File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, 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\Phani\AppData\Local\Programs\Python\Python37-32\drive.py", line 4, in <module>
browser=webdriver.Chrome()
File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Phani\AppData\Local\Programs\Python\Python37-32\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: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
properly installed selenium using pip install
Please Help!!
This isn't working because you haven't provide path of the chrome driver.WebDriver doesn't know where is the chromedriver path.
driver = webdriver.Chrome("path of the chrome driver Chromedriver.exe")
for example:
from selenium import webdriver
driver = webdriver.Chrome("d:/chromedriver/Chromedriver.exe")
driver.get("https://www.google.com")
You can try Firefox for this purpose:
First Download Geckodriver from here: https://github.com/mozilla/geckodriver/releases/tag/v0.24.0
from selenium import webdriver
fox = webdriver.Firefox(executable_path='/path/to/downloaded/gecko/driver')
fox.get("https://www.google.com")
Same could be done with google chrome :
You can download chrome driver from here:
https://sites.google.com/a/chromium.org/chromedriver/home
No need to setup PATH variable.
I have just fixed the issue. You need to download an updated version of Google Chrome. For instance, my old version was 96 but now it is 101. That is it.

Categories