Controlling the exam with Python


We’re asked to learn a library about safety.

However, if the duration is greater than 5 minutes (~300 seconds), we need to click agree to let the timing continue. So, when we finish the learning progress, we’ll click 6*605 = 72 times ……..


Solution (Script)

import time
import selenium
from selenium import webdriver
from import WebDriverWait
from selenium.common.exceptions import TimeoutException
from import expected_conditions as EC

# Please edit:
username = 'your_username'
password = 'your_password'

def get_driver(username,password):
    # Using Chrome to access web
    driver = webdriver.Chrome()
    # Open the website

    # Select the id box
    id_box = driver.find_element_by_name('xuehao')
    # Send id information
    # Find password box
    pass_box = driver.find_element_by_name('password')
    # Send password

    # Find login button
    login_button = driver.find_element_by_name('提交')

    # Find and click on list of courses
    title = '安全知识学习'
    button = driver.find_element_by_css_selector("[title^='"+title+"']")

    # Find the exam we need
    title = '大气物理学院实验室安全考试通用题库'
    button = driver.find_element_by_link_text(title)

    return driver

def accept_alert(driver):
        # Wait for 300 seconds ...
        WebDriverWait(driver, 60*5).until(EC.alert_is_present())
        # Switch to the alert and accept
        alert = driver.switch_to.alert
        print("alert accepted")
    except TimeoutException:
        print("no alert, wait for 5 seconds")

        # Just in case the alert is slow ...
            WebDriverWait(driver, 5).until(EC.alert_is_present())
            alert = driver.switch_to.alert
            print("Finally, alert is accepted!")
        except TimeoutException:
            print("no alert")

def main():
    driver = get_driver(username,password)

    # If lefttime is greater than 300, let python click the pop-up for you.
    k = 0
    while True:
        k += 1
        if k == 72:


if __name__ == '__main__':

Now, we can let it go!


  1. Controlling the Web with Python
  2. Selenium WebDriver Python, search WebElement
  3. Locating Hyperlinks by Link Text
  4. Handling A Confirmation Alert
  5. Check if any alert exists using selenium with python

Say something

Thank you

Your comment has been submitted and will be published once it has been approved.

Click here to see the pull request you generated.


Your comment has not been submitted. Please go back and try again. Thank You!

If this error persists, please open an issue by clicking here.