Nameerror: name ‘webdriver’ is not defined

Are you stuck on this “nameerror: name ‘webdriver’ is not defined” error message?

Keep reading as we will walk you through the process to resolve this error.

This article discusses what this error means, why it occurs, and a guide on how to fix “nameerror: name ‘webdriver’ is not defined.”

What is Webdriver?

A “webdriver” is a tool used in automation testing that allows users to automate web browsers’ actions.

What is “nameerror: name ‘webdriver’ is not defined”?

The “nameerror: name ‘webdriver’ is not defined” is an error message occurs in Python code when you try to use the webdriver module from the selenium package but it was not imported correctly.

This can happen if the line from selenium import webdriver is missing from the code or if the selenium package is not installed correctly on your system.

Why does this error occur?

This error is raised when the Python interpreter is unable to find the “webdriver” variable or function that your code is trying to reference.

“Nameerror: name ‘webdriver’ is not defined” is usually caused by the following:

❌ Incorrect import statements.

❌ Misspelled words.

❌ Missing installations.

❌ An outdated versions of Webdriver or Python.

❌ Syntax errors.

❌ Incorrect file paths.

How to fix “nameerror: name ‘webdriver’ is not defined”?

To fix the “nameerror: name ‘webdriver’ is not defined” error message, you need to check the webdriver installation, correct the import statement, fix the misspelling of ‘webdriver,’ or check for other errors in your code.

Solution 1 : Verify if selenium is installed

Ensure that the selenium package has been installed on your system.

You can check if it’s installed by executing the following command in your command prompt or terminal.

pip show selenium

Solution 2 : Reinstall selenium

When the selenium is not installed or not installed correctly on your system. You can try reinstalling it by executing the following command in your command prompt or terminal.

pip install selenium

If you are using Python 3 you can use the following command:

pip3 install selenium

Solution 3: Import the webdriver module

Ensure to add the line from selenium import “webdriver” at the beginning of your code.

import selenium.webdriver as webdriver
    driver = webdriver.Chrome()

When you have already imported it and still getting the error, it could mean that the selenium package is not installed correctly on your system.

Solution 4: Upgrade the version of the Selenium

Sometimes the error occurs due to a bug or a known issue with the Selenium package.

Upgrading to the latest version can help resolve the error.

pip install --upgrade selenium

Solution 5: Check the location of selenium

Ensure that the “selenium” package is installed in the correct location on your system.

You can check the location by execcuting the following in your Python interpreter.

import selenium
    print(selenium.__file__)

Output:

C:\Users\pies-pc2\PycharmProjects\pythonProject\venv\Lib\site-packages\selenium\__init__.py

Solution 6: Use the webdriver_manager package to automatically install the correct Webdriver executable

The webdriver_manager package can help you automatically install the correct Webdriver executable for your system, which can fix the error.

from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    driver = webdriver.Chrome(ChromeDriverManager().install())

Additional solutions for “nameerror: name ‘webdriver’ is not defined”

When the solutions above do not resolve the error, you can try to execute the following:

Check your code for errors

Ensure that there are no other errors in your code that may be causing the NameError.

You can try running your code in a Python interpreter or using a linter to check for syntax errors.

Check the version of selenium

Ensure that you have a compatible version of selenium installed. You can check the version by executing the following command:

✅  pip show selenium

Also, you have to check the version of Python, if you are using a compatible version of Python with the version of selenium that you have installed.

You check your Python version using the following command:

✅  Python -V

Check your PATH variable

You have to make sure that the location of your Python interpreter and the location of the selenium package are both included in your system’s PATH variable.

Check for conflicts with other packages

If you have other packages that may conflict with selenium, try uninstalling them or creating a new virtual environment to avoid conflict with your installation.

Conclusion

The “nameerror: name ‘webdriver’ is not defined” is an error message occurs in Python code when you try to use the webdriver module from the selenium package but it was not imported correctly.

This article already provides solutions that will help you to fix the “nameerror: name ‘webdriver’ is not defined.”

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊