To set up Selenium, you’ll first need to have Python installed. Check out my other posts if you need help with that.
Next, you need to install Selenium. To do this, simply open up your Windows command prompt or Mac/Linux terminal and enter the command:
1 |
pip install selenium |
Now that Selenium is installed, you need to pick a web browser for Selenium to use. Chrome and Firefox are both just as easy to use with Selenium, but here we’ll look at using Chrome. Assuming you have the Chrome browser installed on your machine, go to the following website:
https://sites.google.com/a/chromium.org/chromedriver/downloads
Follow the link to the latest release and download the appropriate driver for your machine. Unzip the driver and move it to whatever place in your file system you prefer. Just keep in mind you will need to reference the chrome driver’s location when you use Selenium.
Finally, to make sure everything is working as it should, run this code:
1 2 3 4 5 6 7 8 9 10 |
from selenium import webdriver # your executable path is wherever you saved the chrome webdriver # in linux, something like chromedriver = '/home/grayson/Downloads/chromedriver' chromedriver = r'C:\Users\grayson\Downloads\chromedriver.exe" browser = webdriver.Chrome(executable_path=chromedriver) url = 'https://www.duckduckgo.com' browser.get(url) |
If a Chrome browser pops up and takes you to DuckDuckGo’s home page, you’re all set!
If something’s not working, make sure Selenium, Chrome, and chrome driver are all up-to-date and try again. If all else fails, leave a comment and we’ll figure it out.
mark says
hey can you provide me a step by step installation for this. I followed the steps but when i try to install
pip install selenium cmd promt me that pip is not recognized as an internal or external command…
Grayson Stanton says
Hey Mark,
My guess is that Anaconda needs to be added to your PATH environment variable. During the Anaconda installation process, there is a screen that allows you to check a box to do this. It’s near the end of the process, as detailed here. It may be that this box was unchecked by default, and you moved past it without noticing. So if you want to, you can reinstall Anaconda and check that box. If you don’t want to go through installing it again, you can add Anaconda to your PATH manually as detailed here.
Let me know if this doesn’t work.
– Grayson
Julie says
Finally! I’ve read 20 different threads to get my selenium to work with the newest chromedriver and it works. Thank you, thank you, thank you.
Grayson Stanton says
Thanks for your comment Julie, glad it helped!
Jason T. says
I installed selenium with pip3 on osx 10.12
When I try to run a command to call it:
install selenium
I get an error:
Sweetness:Desktop jason$ python bing.py
Traceback (most recent call last):
File “bing.py”, line 1, in
import selenium
ImportError: No module named selenium
Sweetness:Desktop jason$ pip install selenium
Requirement already satisfied: selenium in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (3.141.0)
Requirement already satisfied: urllib3 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from selenium) (1.25.7)
Sweetness:Desktop jason$ python
Any chance someone can help?
Thank you in advance!
Grayson Stanton says
Hi Jason. In the command you copied here, it looks like you used pip, not pip3, to install selenium. Maybe try pip3 install selenium if you haven’t already.
Alternatively, maybe some of the solutions people propose here will prove helpful: https://stackoverflow.com/questions/31147660/importerror-no-module-named-selenium
Anonymous says
Thanks for the reply – I figured it out. I have 2 versions of python on the system. Not sure how to remove the old one. But now things are working when I run: python3 ….
Now I’m working through some other issues. But I think I’ll refrain until I’m truly stumped. It’s amazing how quickly I forget and then remember why I have mad respect for you coders. I could NEVER have the infinite patience required to do what you guys do! I wish I did. But alas…
Thanks again for the reply!
Loren M Speer says
Thanks for trying to make this simple – I am requiring assistance still. I’m running Linux using Mu. I modified your code a bit:
from selenium import webdriver
your executable path is wherever you saved the chrome webdriver
chromedriver = “/lorenmspeer/Downloads/chromedriver.exe”
browser = webdriver.Chrome(executable_path=chromedriver)
url = “https://www.duckduckgo.com”
browser.get(url)
I’m getting this error:
Traceback (most recent call last):
File “/home/lorenmspeer/.local/share/mu/mu_venv-38-20210401-164244/lib/python3.8/site-packages/selenium/webdriver/common/service.py”, line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File “/usr/lib/python3.8/subprocess.py”, line 854, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File “/usr/lib/python3.8/subprocess.py”, line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: ‘/lorenmspeer/Downloads/chromedriver.exe’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/lorenmspeer/mu_code/seleniumTest22.py”, line 5, in
browser = webdriver.Chrome(executable_path=chromedriver)
File “/home/lorenmspeer/.local/share/mu/mu_venv-38-20210401-164244/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py”, line 73, in init
self.service.start()
File “/home/lorenmspeer/.local/share/mu/mu_venv-38-20210401-164244/lib/python3.8/site-packages/selenium/webdriver/common/service.py”, line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver.exe’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Grayson Stanton says
Hi Loren, I believe the problem is that you need to remove the “.exe” file extension from the chromedriver path because you are working in Linux and not Windows. Try a driver path of
chromedriver = "/lorenmspeer/Downloads/chromedriver"