Skip to content

Commit bf5e832

Browse files
author
Haripriya Baskaran
committed
script to automate code submission on codechef
1 parent 813a9be commit bf5e832

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

Python/CodeChef_Selenium/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Automate code submission in codechef through selenium script
2+
The aim of the program is to submit a simple code solution for the problem [COVID RUN](https://www.codechef.com/problems/CVDRUN) to [CodeChef website](https://www.codechef.com/) website using automated script.
3+
4+
### Libraries used:
5+
* [Selenium](https://selenium-python.readthedocs.io/index.html) : A popular automation tool.
6+
* [webdriver-manager](https://pypi.org/project/webdriver-manager/) : Supporting package.
7+
8+
### Pre-requisites:
9+
* **Chromedriver has to be installed before executing the program**
10+
* **An existing account in CodeChef.com**
11+
12+
`>> pip3 install selenium`
13+
14+
`>> pip3 install webdriver-manager`
15+
16+
### Usage:
17+
`>> python codechef_selenium_script.py`
18+
19+
### I/O:
20+
```
21+
Enter codechef username: $(username)
22+
23+
Enter codechef password: $(password)
24+
25+
Enter your solution filename with extension(file must be present in the same directory): $(solution)
26+
27+
Successfully submitted the solution!
28+
```
29+
30+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from selenium import webdriver
2+
from webdriver_manager.chrome import ChromeDriverManager
3+
from time import sleep
4+
from selenium.webdriver.common.action_chains import ActionChains
5+
import os
6+
7+
username = ""
8+
password = ""
9+
solution = ""
10+
driver = None
11+
12+
def open_codechef():
13+
# set url to driver
14+
driver.get("https://www.codechef.com")
15+
16+
def login():
17+
sleep(2)
18+
# find username field
19+
driver.find_element_by_xpath("//*[@id='edit-name']").send_keys(username)
20+
# find password field
21+
driver.find_element_by_xpath("//*[@id='edit-pass']").send_keys(password)
22+
# find Log in button
23+
driver.find_element_by_xpath("//*[@id='edit-submit']").click()
24+
sleep(5)
25+
26+
def go_to_problem():
27+
# Hover over the top bar
28+
action = ActionChains(driver)
29+
practice_probelms_dropdown=driver.find_element_by_xpath("//*[@id='menu-302']/a")
30+
action.move_to_element(practice_probelms_dropdown).perform()
31+
# Click on Practice problems by difficulty level
32+
driver.find_element_by_xpath("//*[@id='menu-303']/a").click()
33+
sleep(2)
34+
# Click on COVID RUN problem
35+
driver.find_element_by_xpath("//*[@id='primary-content']/div/div[2]/div/div[2]/table/tbody/tr[2]/td[1]/div[1]/a").click()
36+
sleep(5)
37+
# Click on Submit button
38+
actions = ActionChains(driver)
39+
pivot = driver.find_element_by_xpath("//*[@id='ember307']/section/div/h2")
40+
actions.move_to_element(pivot).perform()
41+
driver.find_element_by_xpath("//*[@id='problem-statement']/div/a").click()
42+
sleep(5)
43+
44+
def upload_solution():
45+
# scroll down
46+
actions = ActionChains(driver)
47+
pivot = driver.find_element_by_xpath("//*[@id='edit-submit-1']")
48+
actions.move_to_element(pivot).perform()
49+
# click on upload solution button
50+
solution_path = os.getcwd() + "/"
51+
driver.find_element_by_xpath("//*[@id='edit-sourcefile']").send_keys(solution_path+solution)
52+
# Click submit
53+
actions.move_to_element(driver.find_element_by_xpath("//*[@id='cc-footer-div']/div[2]")).perform()
54+
driver.find_element_by_xpath("//*[@id='edit-submit-1']").click()
55+
sleep(15)
56+
57+
58+
def main():
59+
global username
60+
global password
61+
global solution
62+
global driver
63+
# Input from users
64+
username=input("Enter codechef username: ")
65+
password=input("Enter codechef password: ")
66+
solution=input("Enter your solution filename with extension(file must be present in the same directory): ")
67+
# configuring webdriver
68+
driver = webdriver.Chrome(ChromeDriverManager().install())
69+
# Automation begins
70+
open_codechef()
71+
login()
72+
go_to_problem()
73+
upload_solution()
74+
print("Successfully submitted the solution!")
75+
driver.close()
76+
77+
78+
if __name__ == "__main__":
79+
main()

0 commit comments

Comments
 (0)