python – How to let Selenium use the DEFAULT webdriver in operating system?

I would set driver in this function to any web-driver the user may using, through it’s quite hard, Because lots of different web-drivers are be used.

Here’s code:

from selenium import webdriver

def login() -> dict:
    driver = webdriver.ChromiumEdge()
    driver.get("https://an.url")
    cookies = None
    while cookies == None:
        cookies = driver.get_cookie("COOKIES")
        if cookies != None:
            cookies = {cookies["name"]: cookies["value"]}
            driver.close()
            break
    return cookies

I found that when I used driver = selenium.webdriver.Safari() (There’s NO Safari in Windows), it raised selenium.common.exceptions.NoSuchDriverException. I tried to write all web-driver into a list then check if they could raise this exception. However it not happend. It just got stuck. Error log of NoSuchDriverException below.

>>> from selenium import webdriver
>>> driver = webdriver.Safari()
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 67, in _binary_paths
    output = SeleniumManager().binary_paths(self._to_args())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 54, in binary_paths
    return self._run(args)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 126, in _run
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Unsuccessful command executed: C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser safari --language-binding python --output json; code: 65
{'code': 65, 'message': 'safaridriver not available for download', 'driver_path': '', 'browser_path': ''}



The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\safari\webdriver.py", line 48, in __init__
    self.service.path = DriverFinder(self.service, options).get_driver_path()
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 50, in get_driver_path
    return self._binary_paths()["driver_path"]
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 78, in _binary_paths
    raise NoSuchDriverException(msg) from err
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for safari; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

Read more here: Source link