Trouble installing selenium for python

2019-01-28 17:35发布

问题:

I have some trouble with importing selenium webdriver in python. Just to make sure: here is my little script:

import selenium
from selenium import webdriver
browser = webdriver.Firefox(executable_path='/Users/Sleeps/Webdrivers/Firefox/geckodriver')

When I run

from selenium import webdriver,

I am thrown the error

No module named 'selenium.webdriver.firefox.webdriver'.

I neatly follow the steps suggested from Selenium themselves (you can find them here). Nonethelss, I get the error. I have no idea why that is the case. I tried the following:

pip install selenium

python -m pip install -U selenium

sudo pip3 install selenium

No matter what I use, pip confirms

Requirement already satisfied: selenium in ./anaconda/lib/python3.5/site-packages

I further cheked in the Anaconda directory. And I do seem to have the selenium package at the right place: /Users/Sleeps/anaconda/lib/python3.5/selenium/webdriver/__init__.py

EDIT

I further installed the latest gecko driver for MacOS (you can find it here). I unpacked it to Users/Sleeps/Webdrivers/Firefox/.

I ran PATH=$PATH:/Users/Sleeps/Webdrivers/Firefox/geckodriver in the comand line.

Running from pprint import pprint; import os; import sys; pprint(sys.path); pprint(os.getenv("PATH")); pprint(os.getcwd()) returns

['',
 '/Users/Sleeps/anaconda/lib/python3.5/site-packages/six-1.10.0-py3.5.egg',
 '/Users/Sleeps/anaconda/lib/python35.zip',
 '/Users/Sleeps/anaconda/lib/python3.5',
 '/Users/Sleeps/anaconda/lib/python3.5/plat-darwin',
 '/Users/Sleeps/anaconda/lib/python3.5/lib-dynload',
 '/Users/Sleeps/anaconda/lib/python3.5/site-packages',
 '/Users/Sleeps/anaconda/lib/python3.5/site-packages/Sphinx-1.4.1-py3.5.egg',
 '/Users/Sleeps/anaconda/lib/python3.5/site-packages/aeosa',
 '/Users/Sleeps/anaconda/lib/python3.5/site-packages/IPython/extensions',
 '/Users/Sleeps/.ipython']
'/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/Sleeps/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
('/Users/Sleeps/Dropbox/01_Data '
 'Science/Kooperation/EX/Automobile‘)

ls -alh returns

total 24
drwxr-xr-x@  3 Sleeps  staff   102B  9 Aug 12:06 .
drwxr-xr-x@ 35 Sleeps  staff   1,2K  9 Aug 12:06 ..
-rwxr-xr-x@  1 Sleeps  staff   9,4K  9 Aug 11:59 Test.ipynb

When I run from selenium import webdriver, I still get the error. How can I get rid of it?

回答1:

Try below :-

On Ubuntu/Debian systems, this will install pip (and dependencies) and then install the Selenium Python bindings from PyPI:

$ sudo apt-get install python-pip
$ sudo pip install selenium

After the installation, the following code should work:

#!/usr/bin/env python

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')