• python selenium webdriver方法封装(find_element_by)


    下面是对find_element_by_就行了封装,封装之后的高级方法就是getElement()

    下面是具体的代码:

     def getElement(self, selector):
            """
            to locate element by selector
            :arg
            selector should be passed by an example with "i,xxx"
            "x,//*[@id='langs']/button"
            :returns
            DOM 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':
                element = self.driver.find_element_by_id(selector_value)
            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':
                element = self.driver.find_element_by_xpath(selector_value)
            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 type(self, selector, text):
            """
            Operation input box.
    
            Usage:
            driver.type("i,el","selenium")
            """
            el = self.getElement(selector)
            el.clear()
            el.send_keys(text)

    以上的代码是封装了find_element_by_的几种方法,在具体使用封装过的代码的时候,只需要简单的调用即可。

    调用示例

        def login(self, userName, password):
            self.openPage(self.loginPageUrl)
            # self.driver.clearCookies()
            self.driver.implicitlyWait(5)
            self.driver.type("account", userName)
            self.driver.type("password", password)
            self.driver.click("submit")

    自动化的测试代码示例

        def test_ranzhi_login(self):
            """
            测试用例:测试然之登录
            :return:
            """
            # 新建然之的页面对象
            loginPage = RanzhiSubLoginPage(self.autoDriver, self.baseUrl)
    
            # 利用然之的页面对象进行登录
            loginPage.login("admin", "admin")
            sleep(2)
            # 断言 是否登录成功
            self.assertEqual(loginPage.getMainPage(), self.autoDriver.getUrl(), u"登录失败")
  • 相关阅读:
    Linux菜鸟级重点
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
    搭建PHP开发环境
    Struts+Hibernate+Spring实现用户登录功能
    Struts2整合Hibernate3实现用户登录功能
    决战JS(二)
    决战JS
    lightoj-1098
    lightoj-1072
    lightoj-1094 Farthest Nodes in a Tree(求树的直径)
  • 原文地址:https://www.cnblogs.com/111testing/p/6522535.html
Copyright © 2020-2023  润新知