Skip to content

Instantly share code, notes, and snippets.

@mlzboy
Created September 16, 2018 21:55
Show Gist options
  • Save mlzboy/168eeff7a2ed846d1ac0f14afde79dea to your computer and use it in GitHub Desktop.
Save mlzboy/168eeff7a2ed846d1ac0f14afde79dea to your computer and use it in GitHub Desktop.
import time,urllib2
from selenium import webdriver
_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.set_window_size(1200, 10000)
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(3)
driver.execute_script(jquery) #active the jquery lib
driver.execute_script("$('#Button2').click();")
time.sleep(3)
driver.execute_script(jquery) #active the jquery lib
driver.execute_script("$('#Button3').click();")
time.sleep(3)
driver.save_screenshot("c:\code\%s_%s.png"%(i,j))
time.sleep(3)
print "ok",i,j
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment