I am a newbie for selenium python. I have installed python, pip etc..
I am trying to run the below code but it is showing error:
ImportError: cannot import name ‘webdriver’
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
could anyone please solve this issue?
mx0
6,15711 gold badges51 silver badges53 bronze badges
asked Mar 17, 2015 at 6:52
If your file name is selenium.py
, change it to something else and delete .pyc
files or the __pycache__
directory if exists.
If you do not have the selenium, you should install the selenium
by using pip
or pipenv
:
pip install selenium
answered Sep 28, 2015 at 13:18
Mesut GUNESMesut GUNES
6,7542 gold badges30 silver badges48 bronze badges
8
It says that webdriver cant be import.So I assume you have Selenium installed.
So I can only assume that the selenium lied in different place in your path.
Maybe you have accidently create a file named selenium ?
answered Mar 17, 2015 at 7:03
amowamow
2,20911 silver badges19 bronze badges
10
- if your file is selenium.py rename it.
- Pay attention to have import Keys instead of import keys
answered Jun 9, 2020 at 10:00
MatheewMatheew
2553 silver badges12 bronze badges
Step 1: First rename filename if saved with selenium.py
and delete selenium.pyc
.
mv selenium.py test.py
rm selenium.pyc
Step 2: import module selenium if not already installed.
pip install selenium
answered May 16, 2017 at 13:56
deepakdeepak
1,03312 silver badges18 bronze badges
I solved it by reinstalling the older version of selenium package because the newest version doesn’t support Python 2.6.6 which in my case was installed and I didn’t have root access to install the new one.
While the newest version of selenium package doesn’t have support for Python 2.6.6, I had to downgrade by reinstalling the selenium package with lower version
pip uninstall selenium
pip install --user selenium==3.5.0
answered Jun 1, 2018 at 10:33
ljmocicljmocic
1,4871 gold badge16 silver badges23 bronze badges
1
Deepa, it is likely a ‘path not found’ issue. Add as many paths as relevant to help the IDE find selenium and webdriver. For example, on my Mac, I’ve added all the following paths so nothing is missed :
file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5 file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium-3.0.0b2/py file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium-3.0.0b2/py/selenium file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium-3.0.0b2/py/selenium/common file:///Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium-3.0.0b2/py/selenium/webdriver
answered Sep 7, 2016 at 0:57
CVSCVS
131 silver badge6 bronze badges
I have deleted pycache folder and renamed to my py file, now it works without any error.
answered Dec 26, 2020 at 16:11
EnverEnver
5265 silver badges7 bronze badges
———UPDATE SOLVED———-
Fix was I had an file in the same folder named email.py changing the name of this file fixed the issue.
——————————————
I am having issues with the selenium package. I have installed selenium by pip install and ran some code messing with browser. Everything was working and good until I kept getting an error (see below). Couldn’t figure out a fix so I uninstalled selenium reinstalled still had an issue, uninstalled python and deleted its program files and reinstalled same issue. So I reset my pc back to factory settings and reinstalled everything. Was running with no problems… for a couple hours then went back to the same error. No program installs were done nothing was changed it just stopped working. Also below I have the beginning of my code. Any help is greatly appreciated.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome("C:chromedriver_win32chromedriver.exe")
browser.get("https://www.bestbuy.com/site/macbook-air-13-3-laptop-apple-m1-chip-8gb-memory-256gb-ssd-latest-model-space-gray/5721600.p?skuId=5721600")
element = browser.find_element_by_css_selector("h1.v-fw-regular").text
print(element)
The error I am receiving:
Traceback (most recent call last):
File "C:Users_DesktopPython1-26-21_unchanged.py", line 4, in <module>
from selenium import webdriver
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriver__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverfirefoxwebdriver.py", line 29, in <module>
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 27, in <module>
from .remote_connection import RemoteConnection
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremoteremote_connection.py", line 24, in <module>
import urllib3
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesurllib3__init__.py", line 11, in <module>
from . import exceptions
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesurllib3exceptions.py", line 3, in <module>
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesurllib3packagessix.py", line 199, in load_module
mod = mod._resolve()
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesurllib3packagessix.py", line 113, in _resolve
return _import_module(self.mod)
File "C:Users_AppDataLocalProgramsPythonPython39libsite-packagesurllib3packagessix.py", line 82, in _import_module
__import__(name)
File "C:Users_AppDataLocalProgramsPythonPython39libhttpclient.py", line 71, in <module>
import email.parser
File "C:Users_DesktopPythonemail.py", line 13, in <module>
browser = webdriver.Chrome("C:chromedriver_win32chromedriver.exe")
AttributeError: partially initialized module 'selenium.webdriver' has no attribute 'Chrome' (most likely due to a circular import)
audible-activator.py works well, it stops to work when runned from aax2mp3
EDIT:
./aax2mp3_easy.sh <user> <pass> <file>
first run output tail is:
Requirement already satisfied: requests in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: selenium in /usr/local/lib/python2.7/dist-packages
Traceback (most recent call last):
File "audible-activator-feature_login_as_arg/audible-activator.py", line 8, in <module>
from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'
sudo ./aax2mp3_easy.sh <user> <pass> <file>
first run output tail is:
Requirement already satisfied: requests in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: selenium in /usr/local/lib/python2.7/dist-packages
Traceback (most recent call last):
File "audible-activator-feature_login_as_arg/audible-activator.py", line 148, in
fetch_activation_bytes(username, password, options)
File "audible-activator-feature_login_as_arg/audible-activator.py", line 57, in fetch_activation_bytes
executable_path=chromedriver_path)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 75, in init
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in init
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.9.0-6-amd64 x86_64)
2018-03-03 02:19:06+0100 Decoding Player with auth code [*]...
Obviously no updates on both attempts
I am trying to execute some python project and I am getting following error:-
File "feed.py", line 17, in <module>
from selenium import webdriver
ImportError: No module named selenium
I installed selenium package by using pip command for both python2 and python3. If I run python on terminal, I don’t get this error if I try to import selenium.
But project I am executing using venv. I am unable to figure out from where it is trying to look for selenium package.
When I execute python2.7/python/python on terminal and run
from selenium import webdriver
, I don’t see any import errors. But the project when I execute is giving me error. I am unable to locate the path which is being looked up for selenium. How can I find it?
FloT
2,1914 gold badges13 silver badges28 bronze badges
asked Sep 16, 2019 at 10:33
2
You may like to create a virtual environment for Selenium, which is okay, so, do as mentioned earlier. But I think the following is needed to install Selenium with Python on Ubuntu.
Since you are on Python2, so install Selenium like this:
sudo apt-get install python-selenium # for Python2
And, if you want to move to Python3, then replace python-selenium with python3-selenium in the above command. Also, do remember to download Geckodriver for Firefox to further work with Selenium.
wget https://github.com/mozilla/geckodriver/releases/download/v0.20.1/geckodriver-v0.20.1-linux64.tar.gz
You would then need to extract the Geckodriver to a folder such as ~/.local/bin for setting it on your execution PATH.
answered Nov 3, 2019 at 5:37
First activate the virtual environment:
$ source venv/bin/activate
… tthen your terminal will look like this:
(venv)$
Then install selenium:
(venv) $ pip install selenium
Then you will have selenium installed inside the venv
.
The next time when you open a new terminal, just activate the venv
and run your code.
Thomas Ward♦
70.4k29 gold badges173 silver badges236 bronze badges
answered Sep 16, 2019 at 16:03
4
Instead of importing webdriver
you can import your preferred browser from selenium.webdriver
; in my case import Firefox
:
from selenium.webdriver import Firefox
Zanna♦
68.3k55 gold badges210 silver badges320 bronze badges
answered Jul 29, 2021 at 3:35
3