I have a script that used to work with Python 2 and I have upgraded to Python 3. I have been going in circles trying different suggestions for solving my problem but nothing has worked. I get the error:
selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
Here is my current attempt:
.
.
.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options as FirefoxOptions
.
.
.
options=FirefoxOptions()
service=Service(r"c:\cygwin64\geckodriver.exe")
options.add_argument("-headless")
driver=webdriver.Firefox(service=service,options=options)
Can someone help me through this? TIA.
Print the entire error message including the trace information.
Thanks for the reply:
Traceback (most recent call last):
File "C:\cygwin64\home\MyID\spamcop.pyw", line 28, in <module>
driver=webdriver.Firefox(service=service,options=options)
File "C:\Users\MyID\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\firefox\webdriver.py", line 196, in __init__
super().__init__(command_executor=executor, options=options, keep_alive=True)
File "C:\Users\MyID\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\MyID\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\MyID\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\MyID\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
This is line 28:
driver=webdriver.Firefox(service=service,options=options)
Selenium is unable to find the Firefox binary,set corrct path to your
firefox.exe
binary.
Here is working test that i run.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.by import By
# Specify the Firefox binary path
options = FirefoxOptions()
options.binary_location = r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" # Update with your correct path
options.add_argument("-headless")
service = Service(r"C:\cmder\bin\geckodriver.exe")
driver = webdriver.Firefox(service=service, options=options)
url = "https://www.schneider-group.com/en/about/contacts/"
driver.get(url)
heading = driver.find_element(By.CSS_SELECTOR, '#bx_3218110189_13 > p')
print(heading.text)
Output:
Business Center "Yerevan Plaza", Grigor Lusavorich str. 9, Yerevan, 0015, Armenia
Thanks for the reply. Python 2 did not require the location of firefox itself. I didn't know that was needed for python 3. That seems to have gotten me over that hump.
It's usually because Selenium can't locate Firefox properly. Check the geckodriver path and that it matches your system setup. You can explicitly set the path to the Firefox binary.