Search code examples
pythonselenium-webdriverselenium-chromedriver

How do I get selenium to consistently find and use an element when creating a Kahoot quiz?


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 df 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'
password = 'password'

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

df = {
    'Question': ['la explosión', 'el apellido', 'la conferencia', 'la intensidad', 'el paquete'],
    'Answer': ['explosion', 'last name', 'conference', 'intensity', 'package']
}

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.


Solution

  • So I managed to solve the issue myself. I'm not sure if this is the most elegant solution, but essentially I just have the program excecute a while loop until the code mentioned above works here it is:

        for index, row in quiz.iterrows():
        # add new answer type question
        wait = WebDriverWait(driver, 5)
        while True:
            try:
                add_question_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-functional-selector="add-question-button"]')))
                add_question_btn.click()
                break
            except:
                pass
        while True:
            try:
                answer_type_btn = wait.until(
                    EC.element_to_be_clickable(
                        (By.CSS_SELECTOR, 'button[data-functional-selector="create-button__open-ended"]')))
                answer_type_btn.click()
                break
            except:
                pass
    
        # set up question and answer input from spreadsheet
        question_input = row['Question']
        answer_input = row['Answer']
    
        # add questions and answer text to newly created question
        while True:
            try:
                question_text = wait.until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR, 'p[data-placeholder="Start typing your question"]')))
                question_text.send_keys(question_input)
                break
            except:
                pass
        while True:
            try:
                answer_text = wait.until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[placeholder="Type an answer"]')))
                answer_text.send_keys(answer_input)
                break
            except:
                pass