python – Selenium webdriver throwing error on webdriver manager and through direct path

I have tried multiple approaches towards using selenium, but I gete different errors for the same.

Approach 1(Chromedriver is in my directory)

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"Driver/chrome.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)

In this approach, the selenium browser opens up instantly but after 2 minutes I get the error

{
    "name": "WebDriverException",
    "message": "Message: Can not connect to the Service Driver/chrome.exe
",
    "stack": "---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
Cell In[33], line 5
      3 service = Service(r\"Driver/chrome.exe\")
      4 options = webdriver.ChromeOptions()
----> 5 driver = webdriver.Chrome(service=service, options=options)

WebDriverException: Message: Can not connect to the Service Driver/chrome.exe
"
}

Approach 2 –

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")

Here the browser does not open and I get the error
Error 2-

{
    "name": "OSError",
    "message": "[WinError 193] %1 is not a valid Win32 application",
    "stack": "---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[34], line 8
      6 options = Options()
      7 options.add_argument(\"start-maximized\")
----> 8 driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
      9 driver.get(\"https://www.google.com\")

OSError: [WinError 193] %1 is not a valid Win32 application"
}

Please help me in this regard, my versions are
Selenium – 4.23.1
Chrome – 127.0.6533.99

Also, My windows and the selenium driver both are 64 bit

Read more here: Source link