python中selenium中使用ajax_python selenium结合PhantomJS对ajax异步页面进行压测或者爬虫...

#-*- coding: utf-8 -*-

from selenium importwebdriverimporttimeimportrefrom monitor_business importbusinessimportrequestsimportrandomfrom user_config importusername_infofrom selenium.common.exceptions importNoSuchElementExceptionclassautocreateorder():def __init__(self,_username,_loginurl):

self.username=_username

self.loginurl=_loginurldeflogin(self):try:

self.browser= webdriver.PhantomJS(desired_capabilities={'phantomjs.page.settings.resourceTimeout': '15000'})exceptException:print "PhantomJS init fail"self.browser.get(self.loginurl)try:#print "正在登录"

browserlogin = self.browser.find_element_by_class_name("j_login-form")

browserusername= self.browser.find_element_by_id("username-input")

browserusername.click()

browserusername.clear()

browserusername.send_keys(self.username)

browserpassword= self.browser.find_element_by_id("password-input")

browserpassword.click()

browserpassword.clear()

browserpassword.send_keys(u'xxxxx')

browserlogin.submit()#print "登录成功"

exceptNoSuchElementException:print "PhantomJS login fail"

return "fail"

def productaddcart(self,_producturlurl,timeout,jscmd=None):if_producturlurl:

self.browser.get(_producturlurl)print "open product url"time.sleep(timeout)

self.browser.execute_script(jscmd)

n=0

wait_browser=Truewhilewait_browser:try:

self.browser.find_element_by_class_name("joy-ui-dialog-alert-window")

wait_browser=False#print "stop product url check"

exceptNoSuchElementException:if n == 11:print "add cart timeout"self.browser.quit()return "showproductfail"

else:#print "wait time to check product url"

time.sleep(1)

n=n+1

else:print "please config product url"

defshowcart(self):

self.browser.get("http://shop.m.xxxxx.net/shopCart.html")#browser.get_screenshot_as_file("/Users/zhangsongbin/Desktop/screenshot1.png")

print "show cart"n=0

wait_browser=Truewhilewait_browser:try:

self.browser.find_element_by_class_name("order-submit")

wait_browser=FalseexceptNoSuchElementException:if n == 11:print "show cart timeout"self.browser.quit()return "showcartfail"

else:

time.sleep(1)

n=n+1

defcreateorder(self):

jscmd="$('.j_Submit').trigger('click');"self.browser.execute_script(jscmd)#browser.get_screenshot_as_file("/Users/zhangsongbin/Desktop/screenshot1.png")

print "click cart make order"n=0

wait_browser=Truewhilewait_browser:try:

self.browser.find_element_by_class_name("j_Submit")

wait_browser=FalseexceptNoSuchElementException:if n == 11:print "confirm order"self.browser.quit()return "createorderfail"

else:

time.sleep(1)

n=n+1

#browser.get_screenshot_as_file("/Users/zhangsongbin/Desktop/确认订单.png")

time.sleep(5)

jscmd="$('.j_Submit').trigger('click');"self.browser.execute_script(jscmd)defcheckcommission(self):

wait_browser=True

n=0whilewait_browser:

check_order_number=re.findall('',self.browser.page_source)ifcheck_order_number:

wait_browser=False

order_number=check_order_number[0]

self.browser.quit()

requests.get("http://shop.m.xxxxx.net/Fake_Pay?orderNumber="+str(order_number))

time.sleep(30)

check_commission=business.monitor_commission(order_number)#订单的状态检查。if check_commission == "order_fail":print str(order_number)+":commission missing"

else:print str(order_number)+":commission ok"

elif n == 11:print "pay url timeout"self.browser.quit()return "payfail"

else:

time.sleep(1)

n=n+1

defmakeorder():

stratint=0

endint=len(username_info)

inputusername=username_info[random.randrange(stratint,endint)]#我在其他文件中有个用户名的集合,这里是随机调用一个用户出来登录

order=autocreateorder(inputusername,"http://login.xxxxx.net/?service=http%3A%2F%2Fm.xxxxx.net&redirect_uri=%2Fm%2F")

order.login()

order.productaddcart("http://shop.m.xxxxx.net/shop/sku/18613.html",10,jscmd="$('.j_AddCart').trigger('click');")

order.showcart()

order.createorder()

order.checkcommission()defmakemutilorder():

stratint=0

endint=len(username_info)

inputusername=username_info[random.randrange(stratint,endint)]

order=autocreateorder(inputusername,"http://login.xxxxx.net/?service=http%3A%2F%2Fm.xxxxx.net&redirect_uri=%2Fm%2F")

order.login()

productlist=["http://shop.m.xxxxx.net/shop/sku/18613.html","http://shop.m.xxxxx.net/shop/sku/146991.html","http://shop.m.xxxxx.net/shop/sku/149104.html"]for url inproductlist:

productpage=order.productaddcart(url,15,jscmd="$('.j_AddCart').trigger('click');")if productpage == "showproductfail":print "showproductfail"order.showcart()

order.createorder()

order.checkcommission()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值