dimanche 27 septembre 2020

How to select date from bootstrap datepicker with python selenium webdriver?

I'm creating a python automation script to make the online class from a website, as my parents are the teacher so it will help them. I have created the code to login with credentials and go to the right menu, selecting the class, chapter and subject. I'm stuck in selecting the date. When I click in the date option a calendar pops out that's it. I have no idea how to select a date.

There is just this element in HTML for the date menu:

<input name="ctl00$mainContent$txtClassDate" type="date" id="txtClassDate" class="input-group-field TopFont" min="2020-09-27">

Here's my whole code:

from selenium import webdriver
from time import sleep

Input_user = input('User Mobile Number: ')
Input_passwd = input('User Password: ')
Input_class = int(input('Class (Write in digit): '))
while Input_class not in (3, 4, 5):
    print('You only teach classes from 3 to 5, so write 3,4 or 5.')
    Input_class = int(input('Class (Write in digit): '))
Input_chapter = int(input('Chapter Number: '))
while Input_chapter not in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16):
    print('Please select chapter number from 1 to 16')
    Input_chapter = int(input('Chapter Number: '))
Input_time = input('Time hhmmPM/AM: ')

browser = webdriver.Firefox()
browser.get('https://cgschool.in')
browser.maximize_window()

# User field
user = browser.find_element_by_xpath('//*[@id="txtuserid"]').send_keys(Input_user)
sleep(3)

# Password field
passwd = browser.find_element_by_xpath('//*[@id="txtpassword"]').send_keys(Input_passwd)
sleep(3)

try:
    # Login
    login = browser.find_element_by_xpath('//*[@id="btnlogin"]').click()

    # Menu bar
    menu = browser.find_element_by_xpath('/html/body/form/div[3]/div/div[3]/div[1]/div/input').click()
    sleep(3)

    # Teacher's work option under menu
    teacherswork = browser.find_element_by_xpath('/html/body/form/div[3]/div/div[1]/div/ul/li[2]/a').click()
    sleep(3)

    # Online class option under teacher's work 
    onlineclass = browser.find_element_by_xpath('/html/body/form/div[3]/div/div[1]/div/ul/li[2]/ul/li[10]/a').click()
    sleep(3)

    # Selecting class
    kaksha = browser.find_element_by_xpath('//*[@id="ddlClass"]').click()

    if Input_class == int('3'):
        class_3 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[1]/option[4]').click()
        sleep(3)

    elif Input_class == int('4'):
        class_4 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[1]/option[5]').click()
        sleep(3)

    elif Input_class == int('5'):
        class_5 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[1]/option[6]').click()
        sleep(3)


    # Subject field
    subject = browser.find_element_by_xpath('//*[@id="ddlSubject"]').click()
    sleep(3)

    # Selecting english subject
    english = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[2]/option[3]').click()
    sleep(2)
    
    # Chapter field
    chapter = browser.find_element_by_xpath('//*[@id="ddlChapter"]').click()
    sleep(2)

    if Input_chapter == int('1'):
        chap_1 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[2]').click()

    elif Input_chapter == int('2'):
        chap_2 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[3]').click()

    elif Input_chapter == int('3'):
        chap_3 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[4]').click()

    elif Input_chapter == int('4'):
        chap_4 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[5]').click()

    elif Input_chapter == int('5'):
        chap_5 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[6]').click()

    elif Input_chapter == int('6'):
        chap_6 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[7]').click()

    elif Input_chapter == int('7'):
        chap_7 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[8]').click()

    elif Input_chapter == int('8'):
        chap_8 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[9]').click()

    elif Input_chapter == int('9'):
        chap_9 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[10]').click()

    elif Input_chapter == int('10'):
        chap_10 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[11]').click()

    elif Input_chapter == int('11'):
        chap_11 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[12]').click()

    elif Input_chapter == int('12'):
        chap_12 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[13]').click()

    elif Input_chapter == int('13'):
        chap_5 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[14]').click()

    elif Input_chapter == int('14'):
        chap_13 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[15]').click()

    elif Input_chapter == int('15'):
        chap_14 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[16]').click()

    elif Input_chapter == int('16'):
        chap_15 = browser.find_element_by_xpath('/html/body/form/div[4]/div[1]/div[3]/select[3]/option[17]').click()

except:
    print('Something went wrong, please rerun or check your credentials')



Aucun commentaire:

Enregistrer un commentaire