def isTextPresent(self, pattern): driver = startBrowser() text = driver.page_source() #去掉字符串两端的空格,Java使用trim方法 text = text.strip() if pattern in text: return True else: return False
class ElementFinder(object): driver = startBrowser() def find_element(self, type, position): if type == 'xpath': element = self.driver.find_element_by_xpath(position) return element elif type == 'id': element = self.driver.find_element_by_id(position) return element elif type == 'name': element = self.driver.find_element_by_name(position) return element elif type == 'link_text': element = self.driver.find_element_by_link_text(position) return element else: print("不支持的类型") # 定位多个元素方法 def find_elements(self, type, position): if type == 'xpath': element = self.driver.find_elements_by_xpath(position) return element elif type == 'id': element = self.driver.find_elements_by_id(position) return element elif type == 'name': element = self.driver.find_elements_by_name(position) return element elif type == 'link_text': element = self.driver.find_elements_by_link_text(position) return element else: print("不支持的类型") # 截屏方法 def do_screenshot(self): pic_time = time.strftime('%Y%m%d%H%M', (time.localtime())) pic_name = os.path.dirname(os.path.abspath('.')) + '\\picture\\' + pic_time + '.png' self.driver.get_screenshot_as_file(pic_name) logger.info('截屏成功') # # 判断页面元素是否存在的方法 def element_is_exists(self,type,value): flag=True driver=self.driver try: driver.find_element(type,value) return flag except: flag=False return flag # element_str=element.text # if len(element_str) ==0: # self.do_screenshot() # 关闭浏览器的方法 def close(self): self.driver.quit()
def getAllWindowTitles(self): # driver = webdriver.Firefox() driver = startBrowser() #获得当前窗口的句柄 driver.current_window_handle #获得所有窗口的句柄 allhandles = driver.window_handles list = [] for handle in allhandles: driver.switch_to.window(handle) list.append(handle.title()) driver.switch_to.window(allhandles) return len(list)
def getTitle(self): driver = startBrowser() return driver.title()
def open(self): driver = startBrowser() driver.get(u_path) time.sleep(3) logger.info("opened url" + u_path)
def leaveFrame(self): driver = startBrowser() driver.switch_to.default_content() logger.info("left the iframe")
def enterFrame(self, frameID): #python接受任何类型的参数,不必重载 driver = startBrowser() time.sleep(3) driver.switch_to.window(frameID) logger.info("Ebtered inframe" + frameID)
def getWindow(self, i): driver = startBrowser() windows = [] for handle in driver.window_handles: windows.append(handle) driver.switch_to.window(windows[i])
def tabType(self, content): driver = startBrowser() ActionChains(driver).send_keys(content).perform()
class ElementFinder(object): driver = startBrowser() def findElement(self, locator): element = ElementFinder().findElementByPrefix(locator) return element def findElementByPrefix(self, locator): target = locator.strip() if target.startswith('name='): target = target[len('name='):] return self.driver.find_element_by_name(target) elif target.startswith('class='): locator = locator[len('class='):] return self.driver.find_element_by_class_name(locator) elif target.startswith('id='): locator = locator[len('id='):] return self.driver.find_element_by_id(locator) elif target.startswith('link='): locator = locator[len('link='):] return self.driver.find_element_by_link_text(locator) elif target.startswith('css='): locator = locator[len('css='):] return self.driver.find_element_by_css_selector(locator) elif target.startswith('xpath='): locator = locator[len('xpath='):] return self.driver.find_element_by_xpath(locator) elif target.startswith('tag='): locator = locator[len('tag='):] return self.driver.find_element_by_tag_name(locator) else: print(locator + 'can not find element by prefix') return None def find_element(self, type, position): if type == 'xpath': element = self.driver.find_element_by_xpath(position) return element elif type == 'id': element = self.driver.find_element_by_id(position) return element elif type == 'name': element = self.driver.find_element_by_name(position) return element elif type == 'link_text': element = self.driver.find_element_by_link_text(position) return element else: print("不支持的类型") # 定位多个元素方法 def find_elements(self, type, position): if type == 'xpath': element = self.driver.find_elements_by_xpath(position) return element elif type == 'id': element = self.driver.find_elements_by_id(position) return element elif type == 'name': element = self.driver.find_elements_by_name(position) return element elif type == 'link_text': element = self.driver.find_elements_by_link_text(position) return element else: print("不支持的类型")
def mouseoverElement(self, type, position): driver = startBrowser() finder = ElementFinder() element = finder.find_element(type, position) ActionChains(driver).move_to_element(element).perform()
def getHtmlSource(self): driver = startBrowser() return driver.page_source
def getAlert(self): driver = startBrowser() alert = driver.switch_to.alert return alert
def goForward(self): driver = startBrowser() driver.forward()
def goBack(self): driver = startBrowser() driver.back()
def getUrl(self, locator): driver = startBrowser() return driver.current_url
def runJs(self, js): driver = startBrowser() driver.execute_script(js)
def rightClickMouse(self, type, position): driver = startBrowser() finder = ElementFinder() element = finder.find_element(type, position) ActionChains(driver).context_click(element).perform()