|
| 1 | +# Import statements |
| 2 | +from selenium import webdriver |
| 3 | +from webdriver_manager.chrome import ChromeDriverManager |
| 4 | +# time is inbuilt package |
| 5 | +from time import sleep |
| 6 | +# Actions to be perfromed while automation |
| 7 | +from selenium.webdriver.common.action_chains import ActionChains |
| 8 | +import os |
| 9 | + |
| 10 | +# User input declaration |
| 11 | +username = "" |
| 12 | +password = "" |
| 13 | +solution = "" |
| 14 | +driver = None |
| 15 | + |
| 16 | +def open_codechef(): |
| 17 | + # set url to driver |
| 18 | + driver.get("https://www.codechef.com") |
| 19 | + |
| 20 | +def login(): |
| 21 | + sleep(2) |
| 22 | + # find username field |
| 23 | + driver.find_element_by_xpath("//*[@id='edit-name']").send_keys(username) |
| 24 | + # find password field |
| 25 | + driver.find_element_by_xpath("//*[@id='edit-pass']").send_keys(password) |
| 26 | + # find Log in button |
| 27 | + driver.find_element_by_xpath("//*[@id='edit-submit']").click() |
| 28 | + sleep(5) |
| 29 | + |
| 30 | +def go_to_problem(): |
| 31 | + # Hover over the top bar |
| 32 | + action = ActionChains(driver) |
| 33 | + practice_probelms_dropdown=driver.find_element_by_xpath("//*[@id='menu-302']/a") |
| 34 | + action.move_to_element(practice_probelms_dropdown).perform() |
| 35 | + # Click on Practice problems by difficulty level |
| 36 | + driver.find_element_by_xpath("//*[@id='menu-303']/a").click() |
| 37 | + sleep(2) |
| 38 | + # Click on COVID RUN problem |
| 39 | + driver.find_element_by_xpath("//*[@id='primary-content']/div/div[2]/div/div[2]/table/tbody/tr[2]/td[1]/div[1]/a").click() |
| 40 | + sleep(5) |
| 41 | + # Click on Submit button |
| 42 | + actions = ActionChains(driver) |
| 43 | + pivot = driver.find_element_by_xpath("//*[@id='ember307']/section/div/h2") |
| 44 | + actions.move_to_element(pivot).perform() |
| 45 | + driver.find_element_by_xpath("//*[@id='problem-statement']/div/a").click() |
| 46 | + sleep(5) |
| 47 | + |
| 48 | +def upload_solution(): |
| 49 | + # scroll down to a pivot element |
| 50 | + actions = ActionChains(driver) |
| 51 | + pivot = driver.find_element_by_xpath("//*[@id='edit-submit-1']") |
| 52 | + actions.move_to_element(pivot).perform() |
| 53 | + # click on upload solution button |
| 54 | + solution_path = os.getcwd() + "/" |
| 55 | + driver.find_element_by_xpath("//*[@id='edit-sourcefile']").send_keys(solution_path+solution) |
| 56 | + # Click submit |
| 57 | + actions.move_to_element(driver.find_element_by_xpath("//*[@id='cc-footer-div']/div[2]")).perform() |
| 58 | + driver.find_element_by_xpath("//*[@id='edit-submit-1']").click() |
| 59 | + sleep(15) |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + global username |
| 64 | + global password |
| 65 | + global solution |
| 66 | + global driver |
| 67 | + # Input from users |
| 68 | + username=input("Enter codechef username: ") |
| 69 | + password=input("Enter codechef password: ") |
| 70 | + solution=input("Enter your solution filename with extension(file must be present in the same directory): ") |
| 71 | + # configuring webdriver |
| 72 | + driver = webdriver.Chrome(ChromeDriverManager().install()) |
| 73 | + # Automation begins |
| 74 | + open_codechef() |
| 75 | + login() |
| 76 | + go_to_problem() |
| 77 | + upload_solution() |
| 78 | + print("Successfully submitted the solution!") |
| 79 | + driver.close() |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + main() |
0 commit comments