I’m creating a auto type answer Kahoot quiz from a spreadsheet using selenium. Here is an excerpt of some of my code below, and a excerpt to some of the table I’m using. Everything else should work, however on lines 87 to 91 it can’t consistently create a new type answer question and on lines 98 to 101 it can’t consistently type out the questions and answers. When something goes wrong with either it’s because the web element couldn’t be found, which confuses me because the code works properly sometimes.
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import openpyxl
# put in spreadsheet
df = pd.read_excel('1000 of the most common Spanish Nouns.xlsx')
# Type in your email or username
email = '[email protected]'
password = 'peter7144'
folder_name = input('Folder Name: ')
quiz_question_length = int(input('Length of each quiz: '))
# set up the webdriver
driver = webdriver.Chrome()
# go to kahoot login
driver.get('https://create.kahoot.it/auth/login')
# type kahoot account credentials
driver.implicitly_wait(10)
driver.find_element(By.ID, 'username').send_keys(f"{email}")
driver.find_element(By.ID, 'password').send_keys(f"{password}")
# get rid of potential popup
try:
driver.find_element(By.CSS_SELECTOR, 'button[id="onetrust-pc-btn-handler"]').click()
driver.find_element(By.CSS_SELECTOR, 'button[class="ot-pc-refuse-all-handler"]').click()
print('skipped cookie popup')
except:
print('no cookie popup')
# press login
driver.find_element(By.CSS_SELECTOR, 'button[id="login-submit-btn"]').click()
WebDriverWait(driver, 30).until(EC.url_contains("create.kahoot.it"))
# skip second popup
try:
driver.find_element(By.CSS_SELECTOR, 'button[aria-label="Next"]').click()
driver.find_element(By.CSS_SELECTOR, 'button[aria-label="Next"]').click()
driver.find_element(By.CSS_SELECTOR, 'a[class="btn btn--text"]').click()
print('skipped second popup')
except:
print('no second popup')
# go to kahoot account's library
driver.implicitly_wait(10)
driver.find_element(By.CSS_SELECTOR, 'a[data-functional-selector="side-bar-link__library"]').click()
# create a new folder for kahoot quiz
driver.find_element(By.CSS_SELECTOR, 'button[data-functional-selector="library_side-bar_create-folder"]').click()
driver.find_element(By.CSS_SELECTOR, 'input[placeholder="New folder"]').send_keys(f'{folder_name}', Keys.ENTER)
# split df into multiple different parts
quizzes = []
for i in range(0, len(df), quiz_question_length):
if i + quiz_question_length < len(df) + 1:
quizzes.append(df.iloc[i:i + quiz_question_length])
else:
quizzes.append(df.iloc[i:])
# begin creating quizzes
for quiz in quizzes:
# create new kahoot quiz
driver.implicitly_wait(10)
driver.find_element(By.CSS_SELECTOR,
'button[class="button__Button-sc-c6mvr2-0 iwRYhY create-button__PrimaryButton-sc-1wmq7f8-0 jaZrfx"]').click()
driver.find_element(By.CSS_SELECTOR, 'button[aria-label="Create new Kahoot."]').click()
# choose blank option for kahoot quiz
driver.find_element(By.CSS_SELECTOR, 'button[class="styles__FlowOptionInnerContainer-sc-hcxgxq-2 gmFgAG"]').click()
driver.find_element(By.CSS_SELECTOR, 'button[class="flat-button__FlatButton-sc-6uljam-0 iEWtrJ"]').click()
# close the theme panel
driver.find_element(By.CSS_SELECTOR, 'button[aria-label="Close Themes panel"]').click()
# PROBLEM WITH ADDING A NEW TYPE ANSWER QUESTION
for index, row in quiz.iterrows():
# add new answer type question
driver.implicitly_wait(5)
add_question_btn = driver.find_element(By.CSS_SELECTOR, 'button[data-functional-selector="add-question-button"]')
add_question_btn.click()
answer_type_btn = driver.find_element(By.CSS_SELECTOR, 'button[data-functional-selector="create-button__open-ended"]')
answer_type_btn.click()
# set up question and answer input from spreadsheet
question_input = row['Question']
answer_input = row['Answer']
# PROBLEM WITH ADDING QUESTION AND ANSWER TEXT TO QUESTION
question_text = driver.find_element(By.CSS_SELECTOR, 'p[data-placeholder="Start typing your question"]')
question_text.send_keys(question_input)
answer_text = driver.find_element(By.CSS_SELECTOR, 'input[placeholder="Type an answer"]')
answer_text.send_keys(answer_input)
Question Answer
la explosión explosion
el apellido last name
la conferencia conference
la intensidad intensity
el paquete package
la propuesta proposal
la botella bottle
el cadáver corpse
la combinación combination
la dama lady
la onda ripple
la torre tower
el afán eagerness
la comisión commission
la llave faucet
la posesión possession
el candidato candidate
el gas gas
el noviembre November
el talento talent
el té tea
el afecto affection
el consumo consumption
la deuda debt
el horizonte horizon
la patria fatherland
la protesta protest
I think what I need to do is have the webdriver wait until it finds the elements it needs clickable or able to be typed, because the elements are definitely there. I’ve heard of using explicit waits for that however I’m unsure how to go about it. I’m a new coder so this may be a little messy and was just a little project I wanted to work on.
Richmond Nguyen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.