def __init__(self): self.bo = Browser self.bo.click(get_selectors('link_login')) self.bo.input(get_selectors('txt_username'), 'qajjc01') self.bo.input(get_selectors('txt_password'), 'qa1234') time.sleep(1)#由于自动输入用户名密码的速度太快了,导致登录按钮还处于不可点击的状态,所以这边等待1s self.bo.click(get_selectors('btn_login')) self.bo.click(get_selectors('btn_shopping_cart'))
def reduce_amount(self, index): #减少购物车产品的数量,实际操作为点一下购物车中的“-” self.bo.browser.refresh() try: self.bo.wait_element_exist(get_selectors('product_reduce', index)) self.bo.click(get_selectors('product_reduce', index)) time.sleep(1) except: raise '减少按钮未出现'
def add_amount(self, index): #增加购物车产品的数量,实际操作为点一下购物车中的“+” self.bo.browser.refresh() try: self.bo.wait_element_exist(get_selectors('product_add', index)) self.bo.click(get_selectors('product_add', index)) time.sleep(1) except: raise '增加按钮未出现'
def delete_product(self, index): self.bo.browser.refresh() try: self.bo.wait_element_exist(get_selectors('product_delete', index)) self.bo.click(get_selectors('product_delete', index)) self.bo.click(get_selectors('btn_confirm')) time.sleep(1) except: raise '删除按钮未出现'
def get_product_skuid(self, index): ''' 获取购物车中产品对应的skuid ''' category = self.bo.get_text(get_selectors('product_category', index)) name = self.bo.get_text(get_selectors('product_name', index)) products_info = get_product_info() skuid = 0 length=len(products_info.keys()) for i in range(0,length): if name == products_info[i]['name']: if products_info[i]['desc'] in category: skuid = products_info[i]['skuid'] return skuid
def reduce_amount(self, index): if index > 0: self.bo.browser.refresh() try: self.bo.wait_element_exist(get_selectors('product_reduce', index)) self.bo.click(get_selectors('product_reduce', index)) # time.sleep(1) except Exception: logger.error('减少按钮未出现') raise '减少按钮未出现' else: logger.info( '购物车无产品,无法减少数量')
def add_amount(self, index): if index > 0: self.bo.browser.refresh() try: self.bo.wait_element_exist(get_selectors('product_add', index)) self.bo.click(get_selectors('product_add', index)) logger.info('本次第%d个产品的数量增加1' % index) # time.sleep(1) except Exception: raise '增加按钮未出现' logger.error('增加按钮未出现') else: logger.info( 'index:%d'% index) logger.info( '购物车无产品,无法增加数量')
def delete_product(self, index): if index > 0: self.bo.browser.refresh() skuid=self.get_product_skuid(index) logger.info('产品的skuid是%d'% skuid) try: self.bo.wait_element_exist(get_selectors('product_delete', index)) self.bo.click(get_selectors('product_delete', index)) self.bo.click(get_selectors('btn_confirm')) # time.sleep(1) except Exception: logger.error('删除按钮未出现') raise '删除按钮未出现' else: logger.info ('购物车无产品,无法进行删除')
def get_product_skuid(self, index): ''' 获取购物车中产品对应的skuid ''' category = self.bo.get_text(get_selectors('product_category', index)) skuid = 0 for keys in products.keys(): if category == keys.decode('utf-8'): skuid = products[keys]['skuid'] return skuid
def get_text(self,selector): ''' 获取该元素对应的文本内容 ''' self._set_selector(selector) text=self.element.text #注意这个后面不能加()否则会报错 return text def close_browser(self): self.browser.close() global Browser Browser = BrowserOperation() if __name__ == '__main__': username = '******' password = '******' bo = Browser # bo.count_elements("//div[@id='cart-items']/div") print bo.get_text(get_selectors('link_login')) # bo.click(get_selectors('link_login')) # bo.input(get_selectors('txt_username'),username) # bo.input(get_selectors('txt_password'),password)
def get_product_category(self, index): category = self.bo.get_text(get_selectors('product_category', index)) return category
def get_product_name(self, index): name = self.bo.get_text(get_selectors('product_name', index)) return name
def count_products(self): total_amount = self.bo.count_elements(get_selectors('list_product')) return total_amount