Skip to content

Commit 9157c69

Browse files
authored
Merge pull request #224 from HaripriyaB/codechef_selenium
Script to automate code submission on codechef
2 parents 5f10cf7 + ed43ed6 commit 9157c69

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)