Closed – Python isn’t recognizing Selenium module

So I’m working on this website that uses selenium to generate certificates and download them. I installed selenium yesterday as well as webdriver and when I tried to import selenium into my codebase, the compiler doesn’t seem to recognize it. Here is the code to better understand its use:

import pandas as pd
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.keys import Keys
import datetime
import time

# Load the data
cumulativeSummary = pd.read_excel('cumulativeSummary.xls', header=1)
awards = pd.read_csv('specialAwards.csv')

# Remove rows where the 'Scout' column exactly matches 'Guest, Troop 226 Scout'
cumulativeSummary = cumulativeSummary[cumulativeSummary.iloc[:, 0] != 'Guest, Troop 226 Scout']
awards = awards[awards['Scout'] != 'Guest, Troop 226 Scout']

# Remove unnecessary columns from the awards DataFrame
awards = awards.drop(columns=['COH', 'Started'])

# Function to add hyphens where necessary
def add_hyphens(award):
    return re.sub(r'(d+)s+(w+)', r'1-2', award)

# Apply the function to the 'Award' column
awards['Award'] = awards['Award'].apply(add_hyphens)

# Filter the awards DataFrame for camping and service awards
awards = awards[awards['Award'].str.contains('Nights Camping|Hours Service', na=False)]

# Remove rows where both "Earned" and "Awarded" columns are not empty
awards = awards[~(awards['Earned'].notna() & awards['Awarded'].notna())]

# Drop rows where "Awarded" column is not NaN
awards = awards[awards['Awarded'].isna()]

# Drop the "Earned" column
awards = awards.drop(columns=['Earned'])

# Organize awards into categories
camping_awards = awards[awards['Award'].str.contains('Nights Camping', na=False)]
service_awards = awards[awards['Award'].str.contains('Hours Service', na=False)]

def print_awards(category, awards_df):
    results_list = []
    if category == 'camping':
        print("nCamping Awards:")
        days_list = [300, 275, 250, 225, 200, 175, 150, 125, 100, 75, 50, 25, 10]
        awarded_scouts = set()
    elif category == 'service':
        print("nService Awards:")
        hours_list = [100, 75, 50, 25]
        awarded_scouts = set()
    else:
        return results_list

    for award in (days_list if category == 'camping' else hours_list):
        eligible_scouts = awards_df[awards_df['Award'].str.contains(f"{award}-", na=False)]
        if not eligible_scouts.empty:
            print(f"- {award}-{'Nights Camping' if category == 'camping' else 'Hours Service'}:")
            for index, row in eligible_scouts.iterrows():
                if row['Scout'] not in awarded_scouts:
                    print(f"  - {row['Scout']}")
                    awarded_scouts.add(row['Scout'])
                    results_list.append({'Scout': row['Scout'], 'Awards': f"{award}-{'Nights Camping' if category == 'camping' else 'Hours Service'}"})

    return results_list

def determine_camping_awards(cumulative_df, awards_df):
    results_list = []

    # Convert columns to numeric, coerce errors to NaN
    cumulative_df['Camping Nights'] = pd.to_numeric(cumulative_df['Unnamed: 2'], errors='coerce')

    for index, row in cumulative_df.iterrows():
        scout_name = row.iloc[0]
        camping_nights = row['Camping Nights']

        print(f"Processing {scout_name} with {camping_nights} camping nights")

        # Initialize lists for the awards
        highest_award = None
        awarded_lower_awards = []

        # Check for camping awards
        for days in sorted([300, 275, 250, 225, 200, 175, 150, 125, 100, 75, 50, 25, 10], reverse=True):
            award_name = f"{days}-Nights Camping"
            scout_award = awards_df[(awards_df['Scout'] == scout_name) & (awards_df['Award'].str.contains(f"{days}-Nights Camping", na=False))]
            print(f"Checking for {award_name}: Found {not scout_award.empty}")
            if scout_award.empty:
                if not highest_award:
                    highest_award = award_name
            else:
                awarded_lower_awards.append(award_name)

        # Store the results
        if highest_award:
            results_list.append({'Scout': scout_name, 'Awards': highest_award})
            print(f"Assigned {highest_award} to {scout_name}")
        elif awarded_lower_awards:
            results_list.append({'Scout': scout_name, 'Awards': ', '.join(awarded_lower_awards)})
            print(f"Assigned lower awards {', '.join(awarded_lower_awards)} to {scout_name}")

    return results_list

def determine_service_awards(cumulative_df, awards_df):
    results_list = []

    # Convert columns to numeric, coerce errors to NaN
    cumulative_df['Service Hours'] = pd.to_numeric(cumulative_df['Unnamed: 4'], errors='coerce')

    for index, row in cumulative_df.iterrows():
        scout_name = row.iloc[0]
        service_hours = row['Service Hours']

        # Initialize lists for the awards
        highest_award = None
        awarded_lower_awards = []

        # Check for service awards
        for hours in sorted([25, 50, 75, 100], reverse=True):
            award_name = f"{hours}-Hours Service"
            scout_award = awards_df[(awards_df['Scout'] == scout_name) & (awards_df['Award'].str.contains(f"{hours}-Hours Service", na=False))]
            if scout_award.empty:
                if not highest_award:
                    highest_award = award_name
            else:
                awarded_lower_awards.append(award_name)

        # Store the results
        if highest_award:
            results_list.append({'Scout': scout_name, 'Awards': highest_award})
        elif awarded_lower_awards:
            results_list.append({'Scout': scout_name, 'Awards': ', '.join(awarded_lower_awards)})

    return results_list

def create_certificates(awards_list):
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver, 20)

    try:
        for award in awards_list:
            scout_name = award['Scout']
            award_name = award['Awards']
            award_details = f"For {award_name.split('-')[0]} nights."
            today_date = datetime.datetime.today().strftime('%B %d, %Y')

            driver.get("https://certificatemagic.com/personalize.php?design=1")
            print(f"Loaded page for {scout_name}")

            #Add on more code automation

    finally:
        driver.quit()

def main():
    # Display prompt for awards
    print("nWould you like to see the 'camping' awards or 'service' awards? (Type 'camping' or 'service')")
    choice = input().strip().lower()

    # Determine awards based on user choice
    if choice == 'camping':
        camping_awards_list = print_awards('camping', camping_awards)
        create_certificates(camping_awards_list)
    elif choice == 'service':
        service_awards_list = print_awards('service', service_awards)
        create_certificates(service_awards_list)
    else:
        print("Invalid choice. Please type 'camping' or 'service'.")

    # Save the filtered awards DataFrame to a new CSV file
    formatted_csv_path = 'formatted_awards.csv'
    awards.to_csv(formatted_csv_path, index=False)
    print(f"Filtered awards have been saved to {formatted_csv_path}")

if __name__ == "__main__":
    main()

As you can see the only time that I used selenium to automate interactions was in the create_certtificates() function.

I tried to use #type: ignore in order to just ignore the error, but that didn’t seem to work because when i tried to use driver to find for an element, none of the methods for driver didn’t work.

This is what the import section looks like

Any help on how to fix this would be greatly appreciated 🙂

New contributor

Kashyap Sukshavasi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

10

Select the most recent interpreter

New contributor

Kashyap Sukshavasi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật