selenium 智能等待ajax完成调用
selenium 智能等待ajax完成调用 使用webdriverwater 传入一个lambda表达式 判断 ajax调用转台 当 jquery。active==0 表示ajax调用完成 仅在jquery中有效
参数 30 表示最长等待时间 为30秒 超过30秒 调用没完成 报错
参数 driver 是指 一个webdriver对象 eg:driver = webdriver.PhantomJS()
lamda表达式 可以理解为python的匿名函数 s表示的是参数 具体指的就是driver了
WebDriverWait(driver, 30).until(lambda s: s.execute_script("return jQuery.active == 0"))
# -*- coding: utf-8 -*- import random import time import sys import os import mypack.text import mypack.getredis reload(sys) sys.setdefaultencoding('utf8') from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC ABSPATH=os.path.abspath(sys.argv[0]) ABSPATH=os.path.dirname(ABSPATH)+"/" driver = webdriver.PhantomJS() #driver = webdriver.Chrome() driver.implicitly_wait(10) QgoodsId='2003' #n=mypack.text.number(ABSPATH+"txt/1.txt") for i in range(0, 1000): openid=mypack.getredis.openid() url='http://XXXX/goods.php?id='+QgoodsId+'&openid='+openid print url driver.get(url) #formbuy=driver.find_element_by_xpath('//*[@id="ECS_FORMBUY"]/div[2]/ul[2]') #取得颜色的值 知道有多少种颜色 #colortext=formbuy.text #colors=len(colortext.split("\n")) #if colors>2: # Qstring=random.randint(2,colors) #elif colors==2: # Qstring=2 #else: # print '没有颜色' Qstring=random.randint(2,3) #随机点击颜色 Qcolor=driver.find_element_by_xpath('//*[@id="ECS_FORMBUY"]/div[2]/ul[2]/li['+str(Qstring)+']/a') Qcolor.click() WebDriverWait(driver, 30).until(lambda s: s.execute_script("return jQuery.active == 0")) sizeArray=['220','225','230','235','240','245','250'] for i in range(0, 7): Qsize=driver.find_element_by_xpath('//*[@id="'+sizeArray[i]+'"]') act_class=Qsize.get_attribute("class") pflag=False #有class代码没库存 if act_class: continue else: #代码有库存 pflag=True break #如果找不到有尺码的库存则返回 if pflag==False: #print '没有库存' continue Qsize.click() driver.find_element_by_xpath('//*[@id="buy"]').click() try: WebDriverWait(driver, 30).until(lambda driver: driver.current_url.find('**')!=-1 or driver.current_url.find('cart')!=-1) except (ValueError, IndexError) as e: continue if driver.current_url.find('**')!=-1: driver.find_element_by_xpath('/html/body/div[3]/ul/li[1]/button').click() if driver.current_url.find('**')!=-1: continue try: WebDriverWait(driver, 30).until(lambda driver: driver.current_url.find('XXXX')!=-1 or driver.current_url.find('XXX')!=-1) except (ValueError, IndexError) as e: continue if driver.current_url.find('XXX')!=-1: peopername=unicode("***") telphone=unicode("***") address=unicode("***") driver.find_element_by_xpath('//*[@id="theForm"]/div[1]/ul[1]/li[2]/input').send_keys(peopername) driver.find_element_by_xpath('//*[@id="theForm"]/div[1]/ul[2]/li[2]/input').send_keys(telphone) driver.find_element_by_xpath('//*[@id="theForm"]/div[1]/ul[4]/li[2]/input').send_keys(address) driver.find_element_by_xpath('//*[@id="selProvinces"]/option[3]').click() driver.find_element_by_xpath("//*[@id=\"selCities\"]/option[3]").click() driver.find_element_by_xpath("//*[@id=\"selDistricts\"]/option[3]").click() driver.find_element_by_xpath('//*[@id="alter"]').click() # WebDriverWait(driver, 30).until(lambda driver:driver.find_element_by_xpath('/html/body/div[2]/div[2]/button[2]').is_displayed()) # time.sleep(0.5) # driver.find_element_by_xpath('/html/body/div[2]/div[2]/button[2]').click() try: WebDriverWait(driver, 30).until(lambda driver:driver.current_url.find('***')!=-1) except (ValueError, IndexError) as e: continue if driver.current_url.find('***')!=-1: driver.find_element_by_xpath('//*[@id="theForm"]/div[5]/ul/li/button').submit() try: WebDriverWait(driver, 30).until(lambda driver:driver.current_url.find('***')!=-1) except (ValueError, IndexError) as e: continue continue driver.quit()
谢绝转载