How to install selenium in VSCode

In this post, we are going to learn How to install selenium in Visual Studio Code with step by step tutorial. First, we have to install a visual studio code editor and Python. We will also learn how to set up a web driver for chrome, firefox, internet explorer, opera browsers.

How to install selenium python with Microsoft VS Code


To install the selenium with python in visual studio code prerequisites is visual studio code and Python must be installed on the system.

  • The Python must be installed on system.To install python follow these steps.
  • Download Visual Studio from offical website and installed it on your computer.Once the visual code editor is installed flow below steps to steup selenium with python in VS Code

1. Create a folder at a location in your system, We have created folder “vs-selenium-Python”.Open the folder File->Open folder in the visual studio code editor and Create a new file inside this folder with the name “FirstPython.py”.

2. Install the Python extension by clicking on the extension icon left sidebar in Visual code. It provides features (IntelliSense (Pylance), Linting, Debugging (multi-threaded, remote), Jupyter Notebooks, code formatting, refactoring, unit tests).

3. Make sure you have selected the right version of the Python interpreter, by pressing keys ctrl+shift+P –>Python select interpreter, if multiple version of python is installed.

4. Open the visual code Terminal ->new Terminal(ctrl+shift+`) and run this command to install selenium.

pip install selenium 
or 
python -m pip install selenium 

5. Once selenium is installed verify it using the command “pip show selenium”

6. The other option Would be writing this code “from selenium import webdriver” in the ” FirstPython .py” file. Use F5 to run code. If everything works fine our setup for selenium is done.

pip show selenium

#output
Name: selenium
Version: 4.1.0
Summary:
Home-page: https://www.selenium.dev
Author:
Author-email:
License: Apache 2.0
Location: c:\users\Admin\appdata\local\programs\python\python38-32\lib\site-packages
Requires: trio, trio-websocket, urllib3

2. Set up Webdrivers Automatically


We need web drivers for different web browsers, We can set up them by using two options.

  • Download web driver binary ,unzip and setup, Manually
  • Automatically,By using the below steps

We manually download the web driver’s binary and unzip it at some location in the system and Set up the path. It’s tedious!!! On top of that whenever the new version of the driver is released, you need to repeat all steps again and again.

  • Instead of installing every webdriver separately and set up path manually.The webdriver-manager handles it itself
  • The first step is Install the webdriver manager by using command “pip install webdriver-manager” and use it for different web browser .

1. Automate Chrome browser


Write this code in the visual studio code editor in the file ” FirstPython .py” Use F5 to run code and it will launch the chrome browser.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://google.com")
print('driver Title:',driver.title)
print('Driver name:',driver.name)
print('Driver URL:',driver.current_url)
driver.quit()

Output

DevTools listening on ws://127.0.0.1:61058/devtools/browser/fc95d670-ab59-4745-bcc4-6018f427dc8f
driver Title: Google
Driver name: chrome
Driver URL: https://www.google.com/

2. Automate Microsoft Edge


To automate the Microsft Edge browser, Write this code in the visual studio code editor in the file ” FirstPython .py” Use F5 to run code and it will launch the Microsoft Edge browser.

from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(EdgeChromiumDriverManager().install())
driver.maximize_window()
driver.get("https://google.com")
print('driver Title:',driver.title)
print('Driver name:',driver.name)
print('Driver URL:',driver.current_url)
driver.quit()

3.Automate FireFox browser


Mozilla Firefox browser Write this code in the visual studio code editor in the file ” FirstPython .py” Use F5 to run code and it will launch the Microsoft Edge browser.

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

driver.maximize_window()
driver.get("https://google.com")
print('driver Title:',driver.title)
print('Driver name:',driver.name)
print('Driver URL:',driver.current_url)
driver.quit()