所以我正在这个网站上工作,该网站使用 selenium 生成证书并下载它们。我昨天安装了 selenium 以及 webdriver,当我尝试将 selenium 导入我的代码库时,编译器似乎无法识别它。下面是更好地理解其用途的代码:
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()
正如你所看到的,我唯一一次使用 selenium 来自动化交互是在
create_certtificates()
函数中。
我尝试使用
#type: ignore
来忽略错误,但这似乎不起作用,因为当我尝试使用驱动程序查找元素时,driver
的所有方法都不起作用。
任何有关如何解决此问题的帮助将不胜感激:)
选择最近的口译员