How to install selenium in VSCode

In this post, we are going to learn How to install selenium in VSCode 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, and opera browsers.

How to install selenium python with Microsoft VS Code


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

  • The Python must be installed on the system. To install python follow these steps.
  • Download Visual Studio from the official website and installed it on your computer. Once the visual code editor is installed flow below steps to setup 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 to write 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 Web drivers 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, and 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 setting up the path manually. The web driver-manager handles it itself
  • The first step is to Install the web driver manager by using the command “pip install web driver-manager” and use it for the different web browser.

1. Automate the Chrome browser installation and update


Write this code in the visual studio code editor in the file ” FirstPython .py” Use F5 to run the 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 updation


To automate the Microsft Edge browser, Write this code in the visual studio code editor in the file ” FirstPython .py” Use F5 to run the 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 updation and installation


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()