예제 #1
0
 def isTextPresent(self, pattern):
     driver = startBrowser()
     text = driver.page_source()
     #去掉字符串两端的空格,Java使用trim方法
     text = text.strip()
     if pattern in text:
         return True
     else:
         return False
예제 #2
0
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()
예제 #3
0
 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)
예제 #4
0
 def getTitle(self):
     driver = startBrowser()
     return driver.title()
예제 #5
0
 def open(self):
     driver = startBrowser()
     driver.get(u_path)
     time.sleep(3)
     logger.info("opened url" + u_path)
예제 #6
0
 def leaveFrame(self):
     driver = startBrowser()
     driver.switch_to.default_content()
     logger.info("left the iframe")
예제 #7
0
 def enterFrame(self, frameID):  #python接受任何类型的参数,不必重载
     driver = startBrowser()
     time.sleep(3)
     driver.switch_to.window(frameID)
     logger.info("Ebtered inframe" + frameID)
예제 #8
0
 def getWindow(self, i):
     driver = startBrowser()
     windows = []
     for handle in driver.window_handles:
         windows.append(handle)
     driver.switch_to.window(windows[i])
예제 #9
0
 def tabType(self, content):
     driver = startBrowser()
     ActionChains(driver).send_keys(content).perform()
예제 #10
0
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("不支持的类型")
예제 #11
0
 def mouseoverElement(self, type, position):
     driver = startBrowser()
     finder = ElementFinder()
     element = finder.find_element(type, position)
     ActionChains(driver).move_to_element(element).perform()
예제 #12
0
 def getHtmlSource(self):
     driver = startBrowser()
     return driver.page_source
예제 #13
0
 def getAlert(self):
     driver = startBrowser()
     alert = driver.switch_to.alert
     return alert
예제 #14
0
 def goForward(self):
     driver = startBrowser()
     driver.forward()
예제 #15
0
 def goBack(self):
     driver = startBrowser()
     driver.back()
예제 #16
0
 def getUrl(self, locator):
     driver = startBrowser()
     return driver.current_url
예제 #17
0
 def runJs(self, js):
     driver = startBrowser()
     driver.execute_script(js)
예제 #18
0
 def rightClickMouse(self, type, position):
     driver = startBrowser()
     finder = ElementFinder()
     element = finder.find_element(type, position)
     ActionChains(driver).context_click(element).perform()