Python启动Selenium浏览器报错求助:module 'selenium.webdriver'无'Chrome'属性
Hey there, let’s troubleshoot this error step by step—this is a super common issue when working with Selenium, especially with newer versions of the library. Here’s what you can try:
1. Check your Selenium version (most likely culprit)
If you’re using Selenium 4.x or later, the old way of initializing ChromeDriver is deprecated. The webdriver.Chrome() method still works but requires a Service object to specify the driver path, instead of passing the path directly.
First, confirm your Selenium version by running this command in your terminal:
pip show selenium
If it’s version 4.x+, update your code to this:
from selenium import webdriver from selenium.webdriver.chrome.service import Service # Initialize the service with your ChromeDriver path service = Service("D:\\Selenium program\\Chrome\\chromedriver.exe") # Launch Chrome driver = webdriver.Chrome(service=service)
If you’re on Selenium 3.x, skip to the next steps—your original syntax should work, but there’s probably another issue.
2. Avoid filename conflicts
This is a tricky one! If your Python script is named selenium.py, it will override the official Selenium library when you try to import it. So when you write from selenium import webdriver, you’re actually importing your own script instead of the real library, which doesn’t have the Chrome attribute.
Rename your script to something else (like selenium_test.py) and try again.
3. Verify your import statement
Make sure you’re importing the webdriver correctly at the top of your script:
from selenium import webdriver
Avoid using import selenium.webdriver and then selenium.webdriver.Chrome() unless you’re absolutely sure there’s no naming conflict—though the first import style is more standard and less error-prone.
4. Double-check ChromeDriver details
Even if the attribute error isn’t directly related to version mismatches, it’s worth confirming:
- Your ChromeDriver version matches the version of Chrome installed on your machine (check Chrome’s settings > About Chrome for the version number).
- The path
D:\\Selenium program\\Chrome\\chromedriver.exeis correct—make sure the file exists at that location, with no typos in the filename or folder structure.
Give these steps a try, and the error should go away!
内容的提问来源于stack exchange,提问作者Pravanjan




