Python Selenium验证记录已删除

时间:2017-06-01 18:19:57

标签: python selenium

我在python和selenium中有这个代码来查找已删除的记录并验证记录是否已删除。

def find_deleted_device(self, mac, serialno):
    index = 0
    loopNext = True
    matched = False
    while loopNext:
        index = 0
        if not element_locator.find_elements_by_css(driver_obj, self.no_device):
            record = element_locator.find_elements_by_css(driver_obj, self.devices_record)
            macaarray = element_locator.find_elements_by_css(driver_obj, self.device_record_mac_address)
            serialnum = element_locator.find_elements_by_css(driver_obj, self.device_record_serial_number)

            for facility_mac, serialnumber in zip(macaarray, serialnum):
                if facility_mac.text == mac and serialnumber.text == serialno:
                    loopNext = False
                    matched = True
                    break
                elif index == len(record) - 1:
                    if index >= 19:
                        next_page = element_locator.find_element_by_css(driver_obj, self.nextpageselector)
                        if next_page.is_enabled():
                            next_page.click()
                        else:
                            loopNext = False
                    index = index + 1
                else:
                    loopNext = False
        else:
            print('No matching macaddress and serial number, No device message appears')
            assert True
            loopNext = False

    if matched:
        print "The mac and serial number matched, should have been deleted"
        assert False
    else:
        print('No matching macaddress and serial number found')
        assert True
sleep(2)

但问题是有两种情况: 1)如果只存在一条记录而我们删除了那条记录,那么我收到一条消息"没有记录存在" 2)第二种情况是存在一些记录,我可以循环并验证记录是否被删除。

如果是方案1,它可以正常工作。 在情景2的情况下,它通过但是有一个类似" 1496424768.39"的错误。如何逃避此错误。

1 个答案:

答案 0 :(得分:0)

我使用以下替代方法解决了我的问题。

#This method is used to search deleted group
    def find_deleted_device_with_paging(self, mac, serialno):

        page_num = element_locator.find_element_by_css(driver_obj, ".test-grid-pagination-summary")
        if page_num.text < 1:
            print('No matching macaddress and serial number found')
            assert True

        else:
            index = 0
            loopNext = True
            matched = False
            print "The mac and serial number matched, should have been deleted"
            while loopNext:
                index = 0
                record = element_locator.find_elements_by_css(driver_obj, self.devices_record)
                macaarray = element_locator.find_elements_by_css(driver_obj, self.device_record_mac_address)
                serialnum = element_locator.find_elements_by_css(driver_obj, self.device_record_serial_number)

                for facility_mac, serialnumber in zip(macaarray, serialnum):
                    if facility_mac.text == mac and serialnumber.text == serialno:
                        loopNext = False
                        matched = True
                        break
                    elif index == len(record) - 1:
                        if index >= 19:
                            next_page = element_locator.find_element_by_css(driver_obj, self.nextpageselector)
                            if next_page.is_enabled():
                                next_page.click()
                            else:
                                loopNext = False
                        index = index + 1
                    else:
                        loopNext = False

            if matched:
                print "The mac and serial number matched, should have been deleted"
                assert False
            else:
                print('No matching macaddress and serial number found')
                assert True

因此,我没有验证消息,而是将该页面上存在的记录数进行比较以进行分页。这修复了我的代码。