def get_verify_code(self, driver, by): u"""获取图片验证码""" # 验证码图片保存地址 screenImg = verify_code # 浏览器页面截图 driver.get_screenshot_as_file(screenImg) # 定位验证码大小 location = wd.find_element(driver, by).location size = wd.find_element(driver, by).size left = location['x'] top = location['y'] right = location['x'] + size['width'] bottom = location['y'] + size['height'] # 从文件读取截图,截取验证码位置再次保存 img = Image.open(screenImg).crop((left, top, right, bottom)) img.convert('L') # 转换模式:L|RGB img = ImageEnhance.Contrast(img) # 增加对比度 img = img.enhance(2.0) # 增加饱和度 img.save(screenImg) # 再次读取验证码 img = Image.open(screenImg) time.sleep(1) code = pytesseract.image_to_string(img) return code
def js_fours_element(self, driver, by, doc=''): log.logger.info('{0} - 聚焦元素'.format(doc)) try: element = wd.find_element(driver, by) driver.execute_script("arguments[0].scrollIntoView();", element) except: log.logger.error('{0} - 聚焦元素失败!!!'.format(doc))
def move_actionchains_element(self, driver, by, doc=''): log.logger.info('{0} - 鼠标悬停操作'.format(doc)) try: element = wd.find_element(driver, by) ActionChains(self.driver).move_to_element(element).perform() except: log.logger.error('{0} - 鼠标悬停操作 失败!!!'.format(doc)) raise
def get_element_text(self, driver, by, doc=''): log.logger.info('{0} - 获取页面元素:{1}'.format(doc, by)) try: element = wd.find_element(driver, by).text return element except: log.logger.error('{0},页面元素的文本获取失败!!!'.format(doc)) raise
def get_element(self, driver, by, doc=''): log.logger.debug(by) log.logger.info('{0} - 查找页面元素:{1}'.format(doc, by)) try: self.wait_eleVisible(by, doc) return wd.find_element(driver, by) except: log.logger.error('{0},查找页面元素:{1} 失败!!!'.format(doc, by)) raise (NoSuchElementException, TimeoutException)
def select_by_text(self, driver, by, text): u"""通过text定位元素""" element = wd.find_element(driver, by) Select(element).select_by_visible_text(text)
def select_by_value(self, driver, by, value): u"""通过value定位元素""" element = wd.find_element(driver, by) Select(element).select_by_value(value)
def slect_by_index(self, driver, by, index): u"""通过多有index,0开始,定位元素""" element = wd.find_element(driver, by) Select(element).select_by_index(index)