If you work across both PC and MAC to develop Python tests using Selenium you may want to be able to automatically detect which web driver to use.
To do this we can make use of python’s built in “platform” module.
Step 1: Import the web driver module from selenium and the built in os module.
from selenium import webdriver
import platform
Step 2: Detect the system the current Python code is running on.
if platform.system() == "Darwin":
this_is_a = "mac"
elif platform.system() == "Windows":
this_is_a = "pc"
else:
raise Exception("There is an issue detecting this platform")
Step 3: Using the above info we can point to which web driver to use, for arguments sake we’ll aim to use Safari for MAC (has a built in web driver, no additional file required), and Edge for PC.
if platform.system() == "Darwin":
driver = webdriver.Safari()
elif platform.system() == "Windows":
DRIVER_BIN = "..\pathto_webdriver\msedgedriver.exe"
driver = webdriver.Edge(executable_path=DRIVER_BIN)
else:
raise Exception("There is an issue detecting this platform")
From here we can then use the driver to launch the browser and begin creating tests:
driver.get("https://www.your-url.com")