Please tell me how to wrap the code in a try / except block, if an error can occur on each line?
I present two extreme cases. one:
def show_phone(self, link): try: self.driver.get(link); hide_phone_el = self.driver.find_element_by_css_selector(self.hide_phone_selector) hide_phone_el.click() except SomeException as e: print('error', e)
2:
def show_phone(self, link): try: self.driver.get(link); except DriverException as e: print('error', e) try: hide_phone_el = self.driver.find_element_by_css_selector(self.hide_phone_selector) except AttributeException as e: print('error', e) try: hide_phone_el.click() except ClickException as e: print('error', e)
The second case is more reliable, but I find it difficult to imagine a medium / large program written in this style. It will be impossible to read.
The first case does not give a complete picture of the error that occurred. You can create your own type of exception for the first case. However, as a result, it will still be unclear what exactly the error was associated with (with a click, with an assignment operation or with a method error in the driver)
What considerations should be followed when writing this code? Are there best practices?