Created
December 7, 2018 12:43
-
-
Save mlzboy/d4c2581c205d34040b019985f4821a04 to your computer and use it in GitHub Desktop.
teacher-Copy5
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 time,urllib2 | |
from selenium import webdriver | |
def download(driver, target_path): | |
"""Download the currently displayed page to target_path.""" | |
def execute(script, args): | |
driver.execute('executePhantomScript', | |
{'script': script, 'args': args}) | |
# hack while the python interface lags | |
driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute') | |
# set page format | |
# inside the execution script, webpage is "this" | |
page_format = 'this.paperSize = {format: "A4", orientation: "portrait" };' | |
execute(page_format, []) | |
# render current page | |
render = '''this.render("{}")'''.format(target_path) | |
execute(render, []) | |
_init_js=""" | |
(function (){ | |
if (window.__e) | |
{ return; | |
} | |
var e=document.createElement('div'); | |
e.setAttribute("id","__s_msg"); | |
e.style.display="none"; | |
document.body.appendChild(e); | |
window.__e=e; | |
})(); | |
window.__s_set_msg=function(a){ | |
window.__e.setAttribute("msg",a.toString()||""); | |
} | |
""" | |
_loadJsFmt=""" | |
var script = document.createElement('script'); | |
script.src = "{0}"; | |
document.body.appendChild(script); | |
""" | |
_jquery_cdn="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js" | |
_warpjsfmt="__s_set_msg({0})" | |
class ExeJs(object): | |
def __init__(self,driver,trytimes=10): | |
from time import sleep | |
self.driver=driver | |
driver.execute_script(_init_js) | |
while trytimes >0: | |
try: | |
self.msgNode=driver.find_element_by_id('__s_msg') | |
break | |
except Exception: | |
sleep(1) | |
trytimes -= 1 | |
if self.msgNode is None: | |
raise Exception() | |
def exeWrap(self,jsstr): | |
""" jsstr 执行后有返回值,返回值通过self.getMsg()获取 """ | |
self.driver.execute_script(_warpjsfmt.format(jsstr)) | |
def loadJs(self,path): | |
self.execute(_loadJsFmt.format(path)) | |
def loadJquery(self,path=_jquery_cdn): | |
self.loadJs(path) | |
def execute(self,jsstr): | |
self.driver.execute_script(jsstr) | |
def getMsg(self): | |
return self.msgNode.get_attribute('msg') | |
#driver = webdriver.PhantomJS() | |
driver = webdriver.Chrome() | |
driver.set_window_size(1920, 1080) | |
jquery = urllib2.urlopen("http://zjzx.zjnu.edu.cn/bm/jquery-1.7.js").read() | |
for i in range(1,5): | |
for j in range(1,21): | |
driver.get('http://zjzx.zjnu.edu.cn/test/Default.aspx?cid=%s&pid=%s'%(i,j)) | |
exejs = ExeJs(driver) | |
driver.execute_script(jquery) #active the jquery lib | |
driver.execute_script("$('#Button1').click();") | |
time.sleep(2) | |
driver.execute_script(jquery) #active the jquery lib | |
driver.execute_script("$('#Button2').click();") | |
time.sleep(2) | |
driver.execute_script(jquery) #active the jquery lib | |
driver.execute_script("$('#Button3').click();") | |
time.sleep(2) | |
driver.execute_script(jquery) #active the jquery lib | |
# select answer | |
jsss = """ | |
var result=[]; | |
function select(i) | |
{ | |
var str_i = ""; | |
if (i < 9) | |
{ | |
str_i = "0"+ (i+1).toString(); | |
} | |
else | |
{ | |
str_i = (i+1).toString() | |
} | |
var id = "#GridView1_ctl"+str_i+"_Label6"; | |
var answer = $(id).text(); | |
var a_id = "#GridView1_ctl"+str_i+"_Label2"; | |
var a = $(a_id).text(); | |
var b_id = "#GridView1_ctl"+str_i+"_Label3"; | |
var b = $(b_id).text(); | |
var c_id = "#GridView1_ctl"+str_i+"_Label4"; | |
var c = $(c_id).text(); | |
var d_id = "#GridView1_ctl"+str_i+"_Label5"; | |
var d = $(d_id).text(); | |
var question_id = "#GridView1_ctl"+str_i+"_Label1"; | |
var question = $(question_id).text(); | |
var course = $("#drop1 option:selected").text() | |
var exam = $("#drop2 option:selected").text() | |
var data = {"question":question,"a":a,"b":b,"c":c,"d":d,"answer":answer,"course":course,"exam":exam,"no":i.toString()}; | |
data["type"]="single"; | |
console.log(data); | |
//alert(Object.keys(data).length); | |
result.push(data) | |
} | |
function judge(i) | |
{ | |
var str_i = ""; | |
if (i < 9) | |
{ | |
str_i = "0"+ (i+1).toString(); | |
} | |
else | |
{ | |
str_i = (i+1).toString(); | |
} | |
var question_id = "#GridView3_ctl"+str_i+"_Label40"; | |
var question = $(question_id).text(); | |
var course = $("#drop1 option:selected").text() | |
var exam = $("#drop2 option:selected").text() | |
var id = "#GridView3_ctl"+str_i+"_Label41"; | |
var answer = $(id).text(); | |
var data = {"question":question,"answer":answer,"course":course,"exam":exam,"no":i.toString()}; | |
data["type"]="judge"; | |
console.log(data); | |
result.push(data); | |
} | |
function mselect(i) | |
{ | |
var str_i = ""; | |
if (i < 9) | |
{ | |
str_i = "0"+ (i+1).toString(); | |
} | |
else | |
{ | |
str_i = (i+1).toString(); | |
} | |
var question_id = "#GridView2_ctl"+str_i+"_Label15"; | |
var question = $(question_id).text(); | |
var course = $("#drop1 option:selected").text() | |
var exam = $("#drop2 option:selected").text() | |
var id = "#GridView2_ctl"+str_i+"_Label16"; | |
var answer = $(id).text(); | |
var a_id = "#GridView2_ctl"+str_i+"_Label17"; | |
var a = $(a_id).text(); | |
var b_id = "#GridView2_ctl"+str_i+"_Label18"; | |
var b = $(b_id).text(); | |
var c_id = "#GridView2_ctl"+str_i+"_Label19"; | |
var c = $(c_id).text(); | |
var d_id = "#GridView2_ctl"+str_i+"_Label20"; | |
var d = $(d_id).text(); | |
var data = {"question":question,"a":a,"b":b,"c":c,"d":d,"answer":answer,"course":course,"exam":exam,"no":i.toString()}; | |
data["type"]="mutiple"; | |
var e_option_id = "#GridView2_ctl" + str_i + "_Label21"; | |
if ($(e_option_id).text().trim().length != 0) | |
{ | |
var e_id = "#GridView2_ctl"+str_i+"_Label21"; | |
data["e"] = $(e_id).text(); | |
} | |
var f_option_id = "#GridView2_ctl" + str_i + "_Label22"; | |
if ($(f_option_id).text().trim().length != 0) | |
{ | |
var f_id = "#GridView2_ctl"+str_i+"_Label22"; | |
data["f"] = $(f_id).text(); | |
} | |
console.log(data); | |
result.push(data); | |
} | |
for(var x = 1; x <= 40; ++x) | |
{ | |
select(x); | |
} | |
for(var x = 1; x <= 20; ++x) | |
{ | |
mselect(x); | |
} | |
for(var x = 1; x <= 20; ++x) | |
{ | |
judge(x); | |
} | |
$.ajax({ | |
type: "post", | |
url: 'http://192.168.50.116:1323/', | |
async: false, // 使用同步方式 | |
// 1 需要使用JSON.stringify 否则格式为 a=2&b=3&now=14... | |
// 2 需要强制类型转换,否则格式为 {"a":"2","b":"3"} | |
data: JSON.stringify(result), | |
contentType: "application/json; charset=utf-8", | |
dataType: "json", | |
success: function(data) { | |
console.log(data); | |
} // 注意不要在此行增加逗号 | |
}); | |
for(var x=1; x <= 10000; ++x) | |
{ | |
console.log(x); | |
} | |
""" | |
driver.execute_script(jsss); | |
#driver.save_screenshot("c:\code\%s_%s.png"%(i,j)) | |
#download(driver, "c:/code3/%s_%s_answer.pdf"%(i,j)) | |
time.sleep(1) | |
print "ok",i,j | |
print "done!" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment