error while exeutiing Selenium Python automation script - python

I just copied selenium script from web and trying to learn selenium python automation.
Here is the script
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.facebook.com")
While running I got following error
Traceback (most recent call last):
File "C:\Users\hpatel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\hpatel\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\Users\hpatel\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1155, 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\hpatel\python\PythonLearn\src\Example1.py", line 8, in <module>
driver = webdriver.Firefox()
File "C:\Users\hpatel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 160, in __init__
self.service.start()
File "C:\Users\hpatel\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: 'geckodriver' executable needs to be in PATH.
What is the issue?
I copied geckodriver file to c drive and define the path too.

Just replace this line :
driver = webdriver.Firefox()
To:
driver = webdriver.Firefox(executable_path = r'C:/Users/user***/Downloads/geckodriver-v0.20.1-win64/geckodriver.exe')

Related

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

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.

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

Permission error [winerror 5] access is denied-Selenium chrome webdriver

I'm following a tutorial on using selenium and I'm having trouble getting started. Namely, when I try to run the code below, I get the error below. I have seen other users with the same problem, I have tried their solutions, they did not work.
These solutions include:
running pycharm as administrator,
setting permissions for all
group/usernames of subprocess.py and service.py
site-package(and pretty much every file/folder within) to full
access.
from selenium import webdriver
driver = webdriver.Chrome(r"C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\webdriver\chrome")
driver.get("http://python.org")
Here is the full error message:
Traceback (most recent call last): File
"C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\common\service.py",
line 76, in start
stdin=PIPE) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py",
line 775, in init
restore_signals, start_new_session) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py",
line 1178, 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:/Users/User/PycharmProjects/PythonProject/DataCollection", line 2,
in
driver = webdriver.Chrome(r"C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\webdriver\chrome")
File
"C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py",
line 73, in init
self.service.start() File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\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: 'chrome'
executable may have wrong permissions. Please see
https://sites.google.com/a/chromium.org/chromedriver/home
first, replace all \ with /
and then add the executable filename in the file location:
driver = webdriver.Chrome(r'C:/Users/User/AppData/Local/Programs/Python/Python37-32/Lib/site-packages/selenium/webdriver/chrome/chromedriver.exe')

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)

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