0

I am facing a strange issue where my python code takes one minute to complete a loop when i used it under try except statement. The try block contains a driver.find_element which raises exception when it is not found and in the except block i am decrementing the loop count. Could you help me on reducing the looping time as the loops are faster in any programming language.

My code is below

def get_element_by_loop(self,elem_name,prefix,suffix,start_no,end_no):

    '''
    This function will get the loop of the element in parm1 with the given prefix and end_no\n
    parm1 is elem tag name in the id of element
    parm2 is prefix of loop count
    parm3 is start of loop count
    parm4 is end of loop count
    '''

    retry = end_no
    btn_no = start_no
    while retry>0:
        print(f"retry {retry} button no {btn_no}")
        try:
            elem_no = f"//{elem_name}[@id='{prefix}{btn_no}{suffix}']"
            print(btn_no)
            print(datetime.now())
            if self.checkelembyxpath(elem_no):
                print(elem_no)
                elem = self.driver.find_element(By.XPATH, elem_no)
                retry = 0
                return elem
            else:
                raise Exception("Element not found")
        except:
            btn_no+=1
            retry-=1
  • output retry 49 button no 11 11 2023-01-06 12:21:53.116641 [29724:3240:0106/122208.282:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) [29724:3240:0106/122217.878:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) [29724:3240:0106/122231.926:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) [29724:3240:0106/122250.540:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) retry 48 button no 12 12 2023-01-06 12:22:53.187822 [29724:3240:0106/122313.441:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) [29724:3240:0106/122340.717:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) retry 47 button no 13 13 2023-01-06 12:23:53.199986 [29724:3240:0106/122412.320:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) [29724:3240:0106/122448.497:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) retry 46 button no 14 14 2023-01-06 12:24:53.256596 [29724:3240:0106/122529.520:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) retry 45 button no 15 15 2023-01-06 12:25:53.305041 [29724:3240:0106/122614.308:ERROR:component_installer.cc(153)] Move failed.: The system cannot find the path specified. (0x3) retry 44 button no 16 16 2023-01-06 12:26:53.376777

0 Answers0