def enter_promotion(self): try: WebDriverWait(self.driver, 20).until(lambda x: x.find_element_by_xpath('//p[text()="营销推广"]')) self.driver.find_element_by_xpath('//p[text()="营销推广"]').click() except Exception as e: format_print('进入营销推广失败', e) self.take_screenshot('进入营销推广失败')
def enter_app(self, app_id): try: # self.driver.find_element_by_id(app_id).click() # Meizu MX3 self.driver.find_elements_by_id(app_id)[1].click() # Vivo x9 except Exception as e: format_print('进入订单云平台失败 ', e) self.take_screenshot('进入订单云平台失败') sys.exit(0)
def exit(self, eid='com.tencent.mm:id/jc'): try: self.driver.switch_to.context(Setup.context) self.driver.find_element_by_id(eid).click() except Exception as e: format_print('退出应用失败 ', e) self.take_screenshot('退出应用失败') sys.exit(0)
def add_customer(self, xpath='//*[@id="home"]/div[3]/div[2]/a[2]'): try: WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath(xpath)) self.driver.find_element_by_xpath(xpath).click() # 进入新增客户页面 # 填写客户名、联系人、手机号 types = ['小店', '副食', '酒楼', '批发', '烟酒', '便利店', '小卖部', '超市', '百货'] customer_name = create_name() + types[random.randint(0,len(types)-1)] self.driver.find_element_by_id('customer_name').send_keys(customer_name) self.driver.find_element_by_id('contact').send_keys(create_name()) self.driver.find_element_by_id('mobile').send_keys(create_phone()) # 选择客户类型 self.driver.find_element_by_id('default_customer_type').click() typelist = ['零售终端', '批发', '餐饮', '其他', '商超'] types = self.driver.find_elements_by_xpath('/html/body/div[6]/div/div[2]/label') while True: t = int(random.randint(0, len(types)-1)) if types[t].text in typelist: # format_print(t, ' ', types[t].text) # 取到的客户类型 types[t].click() break # 选择客户等级 self.driver.find_element_by_id('subType').click() self.driver.find_element_by_xpath('/html/body/div[6]/div/div[2]/label[1]').click() # 选择配送地址 self.driver.find_element_by_id('delivery-address').click() self.swich_webview(Setup.context) # 切换到APP视图做swipe操作,滑动选择省市区 width = self.driver.get_window_size().get('width') # 获取屏幕宽度 height = self.driver.get_window_size().get('height') # 获取屏幕高度 self.driver.swipe(width*0.25, height*0.9, width*0.25, height*0.1) # 滑动 self.driver.swipe(width*0.9, height*0.9, width*0.9, height*0.7) self.swich_webview(Setup.h5_context) # 切换到H5视图继续后面的操作 self.driver.find_element_by_xpath('//a[text()="完成"]').click() self.driver.find_element_by_id('address').send_keys(create_gbk(8)) # 如果键盘弹出就关闭键盘 # if self.driver.is_keyboard_shown(): # self.driver.hide_keyboard() self.driver.find_element_by_id('add').click() self.driver.find_element_by_xpath('//a[text()="确定"]').click() except Exception as e: format_print('新增客户失败 ', e) self.take_screenshot('新增客户失败') sys.exit(0)
def enter_wechat_official_account(self, account_id, account_name): try: elements = self.driver.find_elements_by_id(account_id) if elements: for e in elements: if e.text == account_name: e.click() break else: format_print('消息列表没有该公众号') except Exception as e: format_print('进入公众号失败 ', e) self.take_screenshot('进入公众号失败') sys.exit(0)
def go_func_group_page(self, delay=3): WebDriverWait(self.driver, 20).until(lambda x: x.find_element_by_id("customerVisits")) self.driver.find_element_by_id("customerVisits").click() # 从首页的客户拜访进入客户列表 time.sleep(1) customers = self.driver.find_elements_by_xpath('//*[@id="customerList"]/div') # 获取当前页客户 if customers: customers[random.randint(0, len(customers)-1)].click() # 随机选一个用户 # time.sleep(3) else: format_print('没有客户,请先添加客户') self.driver.switch_to.frame('layui-layer-iframe1') # 切换到iframe弹层 if self.is_element_exist('/html/body/div[6]/div[3]/a[2]', 'xpath'): # 判断元素节点是否存在 self.driver.find_element_by_xpath('/html/body/div[6]/div[3]/a[2]').click() # 如果出现定位失败弹窗,点击确定 self.driver.find_element_by_id('refresh').click() # 刷新定位 time.sleep(delay)
def swich_webview(self, webview): try: if webview == Setup.context: self.driver.switch_to.context(webview) else: if webview not in self.driver.contexts: self.driver.find_element_by_id('com.tencent.mm:id/jc') self.driver.switch_to.context(webview) # 切换上下文 # format_print(self.driver.current_context) if self.driver.title == '搜一搜': handles = self.driver.window_handles # 获取当前窗口句柄 # format_print(handles) self.driver.switch_to.window(handles[-1]) except Exception as e: format_print('切换视图失败 ', e) self.take_screenshot('切换视图失败') sys.exit(0)
def customer_visit(self): try: # 进入"常规拜访" WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_id("photo")) self.driver.find_element_by_id('photo').click() sleep(1) # 随机拍1-5张照片 for i in range(random.randint(1, 5)): shot_xpath = '//*[@id="form"]/section[2]/div[3]/div/div/div/div/div' WebDriverWait(self.driver, 20).until(lambda x: x.find_element_by_xpath(shot_xpath)) self.driver.find_element_by_xpath(shot_xpath).click() self.swich_webview(Setup.context) # 切换到微信视图控制相机拍照 # self.driver.find_element_by_id('com.android.gallery3d:id/shutter_button').click() # Meizu MX3 # self.driver.find_element_by_id('com.android.gallery3d:id/image_capture_done_img').click() # Meizu MX3 self.driver.find_element_by_id('com.android.camera:id/shutter_button').click() # Vivo x9 拍照 self.driver.find_element_by_id('com.android.camera:id/done_button').click() # Vivo x9 确定 sleep(3) # 等待图片上传完成 self.driver.switch_to.context(Setup.h5_context) WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath('//button[contains(text(),"提交")]')) self.driver.find_element_by_xpath('//button[contains(text(),"提交")]').click() WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath('//a[contains(text(),"确定")]')) self.driver.find_element_by_xpath('//a[contains(text(),"确定")]').click() WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_id("visitEnd")) self.driver.find_element_by_id('visitEnd').click() # 点击拜访完成按钮 WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath('//a[contains(text(),"确定")]')) self.driver.find_element_by_xpath('//a[contains(text(),"确定")]').click() except Exception as e: format_print('拍照出现异常 ', e) self.take_screenshot('拍照异常')
def test_order(self, xpath='//*[@id="home"]/div[3]/div[2]/a[1]', gift=True): try: WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath(xpath)) self.driver.find_element_by_xpath(xpath).click() # 查看合计数量,如果数量不为0,先进购物车清空商品 content = self.driver.find_element_by_id('totalNum').text if content[1] != '0': self.empty_cart() # 选择商品 big_inputs = self.driver.find_elements_by_id( 'bigNum') # 获取所有大单位输入框 small_inputs = self.driver.find_elements_by_id( 'smallNum') # 获取所有小单位输入框 if big_inputs: # 随机选一个商品输入1-5 big_num = random.randint(1, len(big_inputs) - 1) big_inputs[big_num].click() WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath( '/html/body/div[5]/input')) self.driver.find_element_by_xpath( '/html/body/div[5]/input').clear().send_keys( random.randint(1, 5)) self.driver.find_element_by_id('confirm-input').click() sleep(1) small_num = random.randint(1, len(small_inputs) - 1) small_inputs[small_num].click() WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath( '/html/body/div[5]/input')) self.driver.find_element_by_xpath( '/html/body/div[5]/input').clear().send_keys( random.randint(1, 5)) self.driver.find_element_by_id('confirm-input').click() if gift: self.add_gift() WebDriverWait( self.driver, 10).until(lambda x: x.find_element_by_id('placeOrder')) self.driver.find_element_by_id('placeOrder').click() # 下单 WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath( '//*[@id="form"]/div[1]/div[1]')) self.driver.find_element_by_xpath( '//*[@id="form"]/div[1]/div[1]').click() self.swich_webview(Setup.context) # 切换到APP视图做swipe操作 width = self.driver.get_window_size().get('width') # 获取屏幕宽度 height = self.driver.get_window_size().get('height') # 获取屏幕高度 self.driver.swipe(width * 0.5, height * 0.9, width * 0.5, height * 0.1) # 上滑加载更多客户 self.swich_webview(Setup.h5_context) # 切换到H5视图继续后面的操作 sleep(1) customers = self.driver.find_elements_by_id( 'customerInfo') # 获取客户列表 index = random.randint(0, len(customers) - 2) # 这个地方理论上应该-1,但取取到最大值时会报错,-2时不会 customers[index].click() WebDriverWait( self.driver, 10).until(lambda x: x.find_element_by_id('placeOrder')) self.driver.find_element_by_id('placeOrder').click() # 提交订单 WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath( '/html/body/div[4]/div[3]/a[2]')) self.driver.find_element_by_xpath( '/html/body/div[4]/div[3]/a[2]').click() WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath( '/html/body/div[4]/div[2]/a[1]')) self.driver.find_element_by_xpath( '/html/body/div[4]/div[2]/a[1]').click() return len(customers), index except Exception as e: format_print('下单失败 ', e) self.take_screenshot('下单失败') sys.exit(0)
self.driver, 10).until(lambda x: x.find_element_by_id('placeOrder')) self.driver.find_element_by_id('placeOrder').click() # 提交订单 WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath( '/html/body/div[4]/div[3]/a[2]')) self.driver.find_element_by_xpath( '/html/body/div[4]/div[3]/a[2]').click() WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath( '/html/body/div[4]/div[2]/a[1]')) self.driver.find_element_by_xpath( '/html/body/div[4]/div[2]/a[1]').click() return len(customers), index except Exception as e: format_print('下单失败 ', e) self.take_screenshot('下单失败') sys.exit(0) if __name__ == '__main__': order = Order() for i in range(2): # order.test_order(gift=False) format_print(i + 1, ' ', order.test_order(gift=False)) order.go_home()
def take_group_purchase(self): try: # 进入团购直销活动 act_kinds = self.driver.find_elements_by_tag_name('input') act_kinds[1].click() sleep(3) # 选择活动 self.driver.find_element_by_id('activity_title').click() acts = self.driver.find_elements_by_tag_name('label') acts[random.randint(0, len(acts)-1)].click() sleep(1) # 选择费用明细 self.driver.find_element_by_id('expenses_ids').click() expenses = self.driver.find_elements_by_tag_name('label') expenses[random.randint(0, len(expenses)-1)].click() self.driver.find_element_by_xpath('//a[text()="确定"]').click() sleep(1) # 团购单位名称 self.driver.find_element_by_id('companyName').send_keys(create_gbk(5)) # 单位负责人 self.driver.find_element_by_id('customerNames').send_keys(create_name()) # 联系电话 self.driver.find_element_by_id('customerMobile').send_keys(create_phone()) # 赠送数量 self.driver.find_element_by_id('apply_num').clear().send_keys(random.randint(1, 3)) # 赠送金额 if self.is_element_exist('apply_amount', 'id'): if self.driver.find_element_by_id('apply_amount').is_displayed(): amount_list = [0, 10, 20, 50, 100] available_amount = self.driver.find_element_by_id('ableMoney').text[1:-1] # 赠送金额剩余数字 while True: amount = random.sample(amount_list, 1)[0] # 从列表中随机选择一个元素 if amount <= int(available_amount): self.driver.find_element_by_id('apply_amount').clear().send_keys(amount) break # 随机拍1-5张照 for i in range(random.randint(1, 5)): self.driver.find_element_by_class_name('picture1BtnId').click() # self.driver.find_element_by_xpath('//*[@id="form"]/div/section/div[2]/div/div/div/div/div').click() self.swich_webview(Setup.context) # 切换到微信视图控制相机拍照 self.driver.find_element_by_id('com.android.camera:id/shutter_button').click() # Vivo x9 拍照 self.driver.find_element_by_id('com.android.camera:id/done_button').click() # Vivo x9 确定 sleep(600) # 等待图片上传完成 self.driver.switch_to.context(Setup.h5_context) # 切换到H5视图继续操作 WebDriverWait(self.driver, 20).until( lambda x: x.find_element_by_xpath('//button[contains(text(),"提交")]')) self.driver.find_element_by_xpath('//button[contains(text(),"提交")]').click() WebDriverWait(self.driver, 10).until( lambda x: x.find_element_by_xpath('//a[contains(text(),"确定")]')) self.driver.find_element_by_xpath('//a[contains(text(),"确定")]').click() sleep(1) except Exception as e: format_print('拍照出现异常 ', e) self.take_screenshot('拍照异常')
def take_activity(self, delay=1): try: # 进入"活动快捷执行" WebDriverWait(self.driver, 20).until(lambda x: x.find_element_by_id("activity_quick")) self.driver.find_element_by_id('activity_quick').click() # sleep(delay) # 选择活动 WebDriverWait(self.driver, 20).until(lambda x: x.find_element_by_id("activity_title")) self.driver.find_element_by_id('activity_title').click() sleep(0.5) acts = self.driver.find_elements_by_tag_name('label') acts[random.randint(0, len(acts)-1)].click() sleep(delay) # 选择费用明细 self.driver.find_element_by_id('expenses_ids').click() sleep(0.5) costs = self.driver.find_elements_by_tag_name('label') costs[random.randint(0, len(costs)-1)].click() self.driver.find_element_by_xpath('//a[text()="确定"]').click() sleep(delay) # 输入赠送数量 self.driver.find_element_by_id('apply_num').send_keys(random.randint(1, 5)) # 输入赠送金额 if self.is_element_exist('apply_amount', 'id'): if self.driver.find_element_by_id('apply_amount').is_displayed(): amount_list = [0, 10, 200, 50, 100] available_amount = self.driver.find_element_by_id('ableMoney').text[1:-1] # 赠送金额剩余数字 while True: amount = random.sample(amount_list, 1)[0] # 从列表中随机选择一个元素 if amount <= int(available_amount): self.driver.find_element_by_id('apply_amount').clear().send_keys(amount) break # 随机拍1-5张照 for i in range(random.randint(1, 5)): # self.driver.find_element_by_class_name('picture3BtnId').click() self.driver.find_element_by_xpath('//*[@id="form"]/div/section/div[5]/div/div/div/div/div').click() self.swich_webview(Setup.context) # 切换到微信视图控制相机拍照 self.driver.find_element_by_id('com.android.camera:id/shutter_button').click() # Vivo x9 拍照 self.driver.find_element_by_id('com.android.camera:id/done_button').click() # Vivo x9 确定 sleep(3) # 等待图片上传完成 self.driver.switch_to.context(Setup.h5_context) # 切换到H5视图继续操作 WebDriverWait(self.driver, 20).until(lambda x: x.find_element_by_xpath('//button[contains(text(),"提交")]')) self.driver.find_element_by_xpath('//button[contains(text(),"提交")]').click() # 如果出现继续上传的提示,点击继续上传 if self.is_element_exist('//a[contains(text(),"继续上传")]', 'xpath'): self.driver.find_element_by_xpath('//a[contains(text(),"继续上传")]').click() sleep(3) self.driver.find_element_by_xpath('//button[contains(text(),"提交")]').click() WebDriverWait(self.driver, 20).until(lambda x: x.find_element_by_xpath('//a[contains(text(),"确定")]')) self.driver.find_element_by_xpath('//a[contains(text(),"确定")]').click() sleep(delay) WebDriverWait(self.driver, 20).until(lambda x: x.find_element_by_xpath('/html/body/div[2]')) self.driver.find_element_by_xpath('/html/body/div[2]').click() except Exception as e: format_print('拍照出现异常 ', e) self.take_screenshot('拍照异常')
def go_mycenter(self): if self.is_element_exist('.wy-foot-menu>a:last-child'): self.driver.find_element_by_css_selector('.wy-foot-menu>a:last-child').click() else: self.take_screenshot('进入个人中心失败') format_print('进入个人中心失败')