Python : OSError: [WinError 193] %1 is not a valid Win32 application, in selenium

Question:

Problem:
I made a code in python using selenium in pycharm and used it for few days untill I get this error and I scraped all the solution in the internet possible but no luck with that I so I was forces to write my first question to get solution in Stack overflow. Remember Code worked for few days then it give the error.

Note I am a beginner in python and selenium
My code is huge so I will give the part where it gives the error and the setting of the code before running the webdriver

from selenium import webdriver
from selenium.webdriver.edge.options import Options
# from selenium.webdriver.common.keys import Key
from time import sleep
from selenium.webdriver.support.select import Select

options = Options()

options = webdriver.EdgeOptions()
options.add_experimental_option("detach", True)
options.add_argument(r"user-data-dir=C:UsersrealsAppDataLocalMicrosoftEdgeUser Data")
options.add_argument("profile-directory=Profile 1")
options.binary_location = r"C:Program Files (x86)MicrosoftEdgeApplicationmsedge.exe"
options.add_extension(r"C:UsersrealsDownloadsCompressedABS-20230126T194006Z-001ABS.crx")

# create a new Edge driver instance
driver = webdriver.Edge(options=options)

Error :
In above code we have error in this line
driver = webdriver.Edge(options=options)

Full error message

Traceback (most recent call last):
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdrivercommonservice.py", line 195, in _start_process
    self.process = subprocess.Popen(
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsubprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsubprocess.py", line 1438, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
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:UsersrealsAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdrivercommonservice.py", line 88, in start
    self._start_process(self.path)
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdrivercommonservice.py", line 209, in _start_process
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'msedgedriver' executable needs to be in PATH. Please download from https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:UsersrealsPycharmProjectspythonProject1jade.py", line 17, in <module>
    driver = webdriver.Edge(options=options)
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdriveredgewebdriver.py", line 73, in __init__
    super().__init__(
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdriverchromiumwebdriver.py", line 101, in __init__
    self.service.start()
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdrivercommonservice.py", line 100, in start
    self._start_process(path)
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsite-packagesseleniumwebdrivercommonservice.py", line 195, in _start_process
    self.process = subprocess.Popen(
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsubprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:UsersrealsAppDataLocalProgramsPythonPython310libsubprocess.py", line 1438, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
OSError: [WinError 193] %1 is not a valid Win32 application

Response Time
I will try to response ASAP in few hours. Everyone’s help is appreciated

I tried re installing every thing from python to pycharm.
I tried using pythonx32 bit version but no luck.
I tried to use everything outside the code because people explains this error is because somethings is mismatching with your 64 and 32 bit systems but I haven’t tried anything with the code so I am open to do anything to fix this error.

Asked By: FRAMED KING

||

Answers:

Seems like you’re missing msedgedriver.exe.

You can download it on Microsoft and then specify the path with:

from selenium import webdriver
from selenium.webdriver.edge.service import Service

ser = Service("path_to_drivermsedgedriver.exe")

options = webdriver.EdgeOptions()
options.add_experimental_option("detach", True)
options.add_argument(r"user-data-dir=C:UsersrealsAppDataLocalMicrosoftEdgeUser Data")
options.add_argument("profile-directory=Profile 1")
options.binary_location = r"C:Program Files (x86)MicrosoftEdgeApplicationmsedge.exe"
options.add_extension(r"C:UsersrealsDownloadsCompressedABS-20230126T194006Z-001ABS.crx")

driver = webdriver.Edge(service = ser,options = options )

source

Answered By: kaliiiiiiiii