Selenium WebDriver报错:expected str, bytes or os.PathLike object, not Options 问题排查
Hey there, let's break down why you're hitting this error and how to fix it quickly. The issue usually comes from either a missing driver path or outdated library compatibility issues.
Common Causes & Solutions
1. You Forgot to Specify the EdgeDriver Path
When you initialize webdriver.Edge(), if the EdgeDriver executable isn't in your system's PATH environment variable, the method expects you to pass the driver file path first—before the options parameter. Without this path, it misreads your Options object as the driver path, which triggers the error.
Fix:
Add the executable_path argument pointing to your local msedgedriver file (replace the example path with your actual file location):
from selenium import webdriver from msedge.selenium_tools import EdgeOptions from selenium.webdriver.common.keys import Keys from bs4 import BeautifulSoup from time import sleep options = EdgeOptions() options.use_chromium = True options.headless = True print('- Finish importing packages') # Update this path to match your msedgedriver location driver = webdriver.Edge(executable_path='C:/Users/YourName/Downloads/msedgedriver.exe', options=options)
Pro tip: Make sure your msedgedriver version exactly matches your installed Edge browser version—this avoids tons of weird compatibility bugs.
2. You're Using Outdated Library Tools (Selenium 4.x+)
If you're running Selenium 4.0 or newer, the msedge.selenium_tools library is no longer needed. Selenium now has native, built-in support for Edge Chromium, and mixing the old library with the new Selenium version causes type mismatches with the webdriver.Edge() method.
Fix:
Switch to Selenium's official EdgeOptions and ditch the deprecated library:
from selenium import webdriver from selenium.webdriver.edge.options import Options from selenium.webdriver.common.keys import Keys from bs4 import BeautifulSoup from time import sleep options = Options() options.add_argument("--headless=new") # Updated headless flag for modern Selenium print('- Finish importing packages') # If msedgedriver is in your system PATH, no need for executable_path here driver = webdriver.Edge(options=options)
This uses Selenium's maintained, native Edge integration, which eliminates the type error right away.
Quick Final Checks
- Double-check your
executable_pathfor typos (even a small typo can break things) - Confirm your Edge browser and msedgedriver versions are identical—this is non-negotiable for smooth operation
内容的提问来源于stack exchange,提问作者hacking_mike

