Last active
November 17, 2020 16:30
-
-
Save VinhDevNguyen/e5c1066f467167de05a77ed6a96daacd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import string | |
from selenium import webdriver | |
import time | |
def get_random_string(length): | |
letters = string.ascii_lowercase | |
result_str = ''.join(random.choice(letters) for i in range(length)) | |
return(result_str) | |
def get_random_digit(length): | |
letters_and_digits = string.digits | |
result_str = ''.join((random.choice(letters_and_digits) for i in range(length))) | |
return(result_str) | |
def check_if_string_in_file(file_name, string_to_search): | |
""" Check if any line in the file contains given string """ | |
# Open the file in read only mode | |
with open(file_name, 'r', encoding="utf-8") as read_obj: | |
# Read all lines in the file one by one | |
for line in read_obj: | |
# For each line, check if line contains the string | |
if string_to_search in line: | |
return True | |
return False | |
Run = True | |
nth = 1 | |
while(Run): | |
url = "http://tanphu.khoahoctre.com.vn/login/signup.php?" | |
driver = webdriver.Edge(executable_path= r'D:/Tool/edgedriver_win64(1)/msedgedriver.exe') | |
driver.get(url) | |
# Tao tai khoan | |
# Ten dang nhap | |
tendangnhap = get_random_string(10) | |
password = get_random_string(10) | |
driver.find_element_by_id('id_username').send_keys(tendangnhap) | |
time.sleep(0.1) | |
driver.find_element_by_id('id_password').send_keys(password) | |
time.sleep(0.1) | |
driver.find_element_by_id('id_password2').send_keys(password) | |
time.sleep(0.1) | |
email = get_random_string(10) + '@gmail.com' | |
driver.find_element_by_id('id_email').send_keys(email) | |
time.sleep(0.1) | |
driver.find_element_by_id('id_email2').send_keys(email) | |
time.sleep(0.1) | |
driver.find_element_by_id('id_firstname').send_keys(tendangnhap) | |
time.sleep(0.1) | |
# TT lien he | |
so_CMND = get_random_digit(12) | |
don_vi = get_random_string(10) | |
driver.find_element_by_id('id_profile_field_cmnd').send_keys(so_CMND) | |
time.sleep(0.1) | |
driver.find_element_by_id('id_profile_field_dienthoai').send_keys(get_random_digit(9)) | |
time.sleep(0.1) | |
driver.find_element_by_id('id_profile_field_donvi').send_keys(don_vi) | |
time.sleep(0.1) | |
driver.find_element_by_id('id_submitbutton').click() | |
time.sleep(0.2) | |
# Dang nhap voi tai khoa vua tao | |
driver.find_element_by_class_name('btn-login').click() | |
time.sleep(2.5) | |
driver.find_element_by_id('username').send_keys(tendangnhap) | |
time.sleep(0.1) | |
driver.find_element_by_id('password').send_keys(password) | |
time.sleep(0.1) | |
driver.find_element_by_id('loginbtn').click() | |
time.sleep(2) | |
# Vo bai thi | |
driver.find_element_by_class_name('instancename').click() | |
time.sleep(0.2) | |
driver.find_element_by_xpath('//*[@value="Bắt đầu kiểm tra"]').click() | |
driver.find_element_by_id('id_submitbutton').click() | |
# Lay cau hoi | |
for i in range(1,20): | |
button = 'quiznavbutton' + str(i) | |
driver.find_element_by_id(button).click() | |
cau_hoi = driver.find_element_by_class_name('qtext').text | |
dap_an = driver.find_element_by_class_name('ablock').text | |
if check_if_string_in_file('De_Thi.txt', cau_hoi) == False: | |
with open('De_Thi.txt', "a+", encoding="utf-8") as f: | |
f.writelines('\n') | |
f.writelines(cau_hoi) | |
f.writelines(dap_an) | |
f.close() | |
print("Xong de " + nth) | |
nth = nth +1 | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment