def click(self, selector): element = self.find_element(selector) try: element.click() logger.info("The element \' %s \' was clicked." % element.text) except NameError as e: logger.error("Failed to click the element with %s" % e)
def clear(self, selector): element = self.find_element(selector) try: element.clear() logger.info("Clear text in input box before typing.") except NameError as e: logger.error("Failed to clear in input box with %s" % e) self.get_windows_img()
def type(self, selector, text): element = self.find_element(selector) element.clear() try: element.send_keys(text) logger.info("Had type \' %s \' in inputBox" % text) except NameError as e: logger.error("Failed to type in input box with %s" % e) self.get_windows_img()
def find_element(self, selector): """ 这个地方为什么是根据=>来切割字符串,请看页面里定位元素的方法 submit_btn = "id=>su" login_lnk = "xpath => //*[@id='u1']/a[7]" # 百度首页登录链接定位 如果采用等号,结果很多xpath表达式中包含一个=,这样会造成切割不准确,影响元素定位 :param selector: :return: element """ element = '' if '=>' not in selector: return self.driver.find_element_by_id(selector) selector_by = selector.split('=>')[0] selector_value = selector.split('=>')[1] if selector_by == "i" or selector_by == 'id': try: element = self.driver.find_element_by_id(selector_value) logger.info("Had find the element \' %s \' successful " "by %s via value: %s " % (element.text, selector_by, selector_value)) except NoSuchElementException as e: logger.error("NoSuchElementException: %s" % e) self.get_windows_img() # take screenshot elif selector_by == "n" or selector_by == 'name': element = self.driver.find_element_by_name(selector_value) elif selector_by == "c" or selector_by == 'class_name': element = self.driver.find_element_by_class_name(selector_value) elif selector_by == "l" or selector_by == 'link_text': element = self.driver.find_element_by_link_text(selector_value) elif selector_by == "p" or selector_by == 'partial_link_text': element = self.driver.find_element_by_partial_link_text( selector_value) elif selector_by == "t" or selector_by == 'tag_name': element = self.driver.find_element_by_tag_name(selector_value) elif selector_by == "x" or selector_by == 'xpath': try: element = self.driver.find_element_by_xpath(selector_value) logger.info("Had find the element \' %s \' successful " "by %s via value: %s " % (element.text, selector_by, selector_value)) except NoSuchElementException as e: logger.error("NoSuchElementException: %s" % e) self.get_windows_img() elif selector_by == "s" or selector_by == 'selector_selector': element = self.driver.find_element_by_css_selector(selector_value) else: raise NameError("Please enter a valid type of targeting elements.") return element
def get_windows_img(self): """ 在这里我们把file_path这个参数写死,直接保存到我们项目根目录的一个文件夹.\Screenshots下 """ file_path = os.path.dirname(os.path.abspath('.')) + '/screenshots/' rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) screen_name = file_path + rq + '.png' try: self.driver.get_screenshot_as_file(screen_name) logger.info( "Had take screenshot and save to folder : /screenshots") except NameError as e: logger.error("Failed to take screenshot! %s" % e) self.get_windows_img()
def quit_browser(self): logger.info("Now, Close and quit the browser.") self.driver.quit()
def open_browser(self, driver): config = configparser.ConfigParser() file_path = os.path.dirname( os.path.abspath('.')) + '/config/config.ini' config.read(file_path) browser = config.get("browserType", "browserName") logger.info("You had select %s browser." % browser) url = config.get("testServer", "URL") logger.info("The test server url is: %s" % url) if browser == "Firefox": driver = webdriver.Firefox() logger.info("Starting firefox browser.") elif browser == "Chrome": driver = webdriver.Chrome(self.chrome_driver_path) logger.info("Starting Chrome browser.") elif browser == "IE": driver = webdriver.Ie(self.ie_driver_path) logger.info("Starting IE browser.") driver.get(url) logger.info("Open url: %s" % url) driver.maximize_window() logger.info("Maximize the current window.") driver.implicitly_wait(10) logger.info("Set implicitly wait 10 seconds.") return driver
def close(self): try: self.driver.close() logger.info("Closing and quit the browser.") except NameError as e: logger.error("Failed to quit the browser with %s" % e)
def wait(self, seconds): self.driver.implicitly_wait(seconds) logger.info("wait for %d seconds." % seconds)
def back(self): self.driver.back() logger.info("Click back on current page.")
def forward(self): self.driver.forward() logger.info("Click forward on current page.")
def sleep(seconds): time.sleep(seconds) logger.info("Sleep for %d seconds" % seconds)
def get_page_title(self): logger.info("Current page title is %s" % self.driver.title) return self.driver.title